[mochikit] Re: Introducing the MochiKit.Text module
Hi Per, On Wed, Dec 17, 2008 at 12:23, Per Cederberg wrote: > I've just finished committing one of my planned additions to MochiKit > 1.5 -- a new MochiKit.Text module: Excellent work, thank you! > 1. Collect more ideas and feedback (hence this email). One comment about startsWith, endsWith, contains etc.. currently they take first the haystack and then the needle (I'm talking about parameter order). Perhaps switching this could benefit the usecase where this is used with partial? my_namesakes = filter(partial(contains, 'Arnar'), list_of_names); If the generalized partial I posted a few days ago this doesn't matter, as it would be my_namesakes = filter(partial(contains, __, 'Arnar'), list_of_names); This reminds me of another idea, since it's unrelated to the text module I'll post it in a different thread. The module should define constants like ascii_chars, digits etc. as per http://www.python.org/doc/2.3/lib/module-string.html -- also split, join (although they are included in js as native methods - having functions can be convenient for map & friends). For padRight and padLeft - I prefer the Python names ljust, rjust (and center). cheers, Arnar --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Curry and uncurry
One thing I think could be useful is to port Haskell's curry and uncurry. This is basically a convenience method for (un)wrapping an .apply on a function object: function curry(f) { return function () { // first convert arguments to a regular array var args = Array.prototype.slice.call(arguments); return f(args); } } function uncurry(f) { return function (args) { return f.apply(this, args); } } Example use: test = [ [10, 1], [20, 2], [30, 3] ]; assertEqual(map(uncurry(operator.plus), test), [11, 22, 33]); // assume join is a function that takes a list and returns a string // with the elements joined with some delimiter f = curry(partial(join, _, ", ")) assert(f("Bond", "James Bond") == "Bond, James Bond") Does anyone else think this could be useful? What module would it fit? Base already has a lot of functional stuff (compose, partial, map & friends) - I'm wondering if it fits there or if all the functional stuff should be in a seperate module MochiKit.Functional - as Python seems to be heading. cheers, Arnar --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Introducing the MochiKit.Text module
On Wed, Dec 17, 2008 at 5:11 PM, Arnar Birgisson wrote: > One comment about startsWith, endsWith, contains etc.. currently they > take first the haystack and then the needle (I'm talking about > parameter order). Perhaps switching this could benefit the usecase > where this is used with partial? > > my_namesakes = filter(partial(contains, 'Arnar'), list_of_names); This is a good point. I think I'll change my ways. Watch out for the next commit... :-) > The module should define constants like ascii_chars, digits etc. as > per http://www.python.org/doc/2.3/lib/module-string.html -- also > split, join (although they are included in js as native methods - > having functions can be convenient for map & friends). That might be an idea. With the same "reversed" parameter order as the ones above of course. > For padRight and padLeft - I prefer the Python names ljust, rjust (and > center). Ah, well... I didn't look too hard into Python here. Just googled various name ideas and picked the most popular one (think it was from some MS API actually)... ;-) Cheers, /Per --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Introducing the MochiKit.Text module
Hi everyone, I've just finished committing one of my planned additions to MochiKit 1.5 -- a new MochiKit.Text module: http://www.mochikit.com/doc/html/MochiKit/Text.html It basically provides some of the text formatting stuff discussed here on the list previously. With some random string utility functions added on top. I chose the name "Text" instead of "String" or "Format" for various reasons, but in the end I guess it is just a matter of taste. Also, it avoids collisions with the "MochiKit.String" name already in use by Amit. My plans for MochiKit.Text are as follows: 1. Collect more ideas and feedback (hence this email). 2. Migrate camelize, strip, lstrip, rstrip, truncToFixed, roundToFixed and locale handling over to MochiKit.Text. Perhaps the remaining parts of MochiKit.Format should be deprecated? 3. Implement some additional stuff like toTitleCase, isDigit, isAlphaNumeric, etc. Please let me know what you think. Cheers, /Per --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Introducing the MochiKit.Text module
Hi, On Wed, Dec 17, 2008 at 17:32, Per Cederberg wrote: > Ah, well... I didn't look too hard into Python here. Just googled > various name ideas and picked the most popular one (think it was from > some MS API actually)... ;-) That's not such a bad method :) However, I feel MochiKit draws heavily on Python (or Python libraries) for a lot of things, so to me it makes sense to keep this convention. cheers, Arnar --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Curry and uncurry
I think, for functional programming, we should keep things simple. It's better to keep code simple and readable. Can you explain few more use cases of the proposed functions? Regards -- Amit On Dec 17, 9:22 pm, "Arnar Birgisson" wrote: > One thing I think could be useful is to port Haskell's curry and > uncurry. This is basically a convenience method for (un)wrapping an > .apply on a function object: > > function curry(f) { > return function () { > // first convert arguments to a regular array > var args = Array.prototype.slice.call(arguments); > return f(args); > } > > } > > function uncurry(f) { > return function (args) { > return f.apply(this, args); > } > > } > > Example use: > test = [ [10, 1], > [20, 2], > [30, 3] ]; > > assertEqual(map(uncurry(operator.plus), test), [11, 22, 33]); > > // assume join is a function that takes a list and returns a string > // with the elements joined with some delimiter > > f = curry(partial(join, _, ", ")) > assert(f("Bond", "James Bond") == "Bond, James Bond") > > Does anyone else think this could be useful? What module would it fit? > Base already has a lot of functional stuff (compose, partial, map & > friends) - I'm wondering if it fits there or if all the functional > stuff should be in a seperate module MochiKit.Functional - as Python > seems to be heading. > > cheers, > Arnar --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Introducing the MochiKit.Text module
It's very good addition :) especially the new formating functions. I'll improve my MochiKit.String module so that one can create String instance with the new formating patterns. Again, I'm too, agree with Arner. Try to follow Python conventions as much as possible. startsWith => startswith endsWith => endswith and so on... See `MochiKit.String.splitlines`, you can remove `splitJoin` as you one can easily do the same with `splitlines` (if it's all about triming newline chars). Regards -- Amit On Dec 17, 5:23 pm, "Per Cederberg" wrote: > Hi everyone, > > I've just finished committing one of my planned additions to MochiKit > 1.5 -- a new MochiKit.Text module: > > http://www.mochikit.com/doc/html/MochiKit/Text.html > > It basically provides some of the text formatting stuff discussed here > on the list previously. With some random string utility functions > added on top. I chose the name "Text" instead of "String" or "Format" > for various reasons, but in the end I guess it is just a matter of > taste. Also, it avoids collisions with the "MochiKit.String" name > already in use by Amit. > > My plans for MochiKit.Text are as follows: > > 1. Collect more ideas and feedback (hence this email). > > 2. Migrate camelize, strip, lstrip, rstrip, truncToFixed, roundToFixed > and locale handling over to MochiKit.Text. Perhaps the remaining parts > of MochiKit.Format should be deprecated? > > 3. Implement some additional stuff like toTitleCase, isDigit, > isAlphaNumeric, etc. > > Please let me know what you think. > > Cheers, > > /Per --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Curry and uncurry
The names "curry" and "uncurry" were a bit confusing to me, so it took me a while to understand these two functions... http://en.wikipedia.org/wiki/Currying To me (and probably other non-Haskell users) the names imply the same thing as bind or partial. It's a confusing world... :-( In JavaScript, I think plain apply + MochiKit.Base.bind does the same thing: var test = [ [10, 1], [20, 2], [30, 3] ]; var addArray = bind("apply", operator.add, null); assertEqual(map(addArray, test), [11, 22, 33]); It's no beauty, so perhaps this particular variant of bind merits an alias? Uncurrying is just the same as the built-in apply function, so that seems unnecessary. Cheers, /Per On Wed, Dec 17, 2008 at 5:22 PM, Arnar Birgisson wrote: > > One thing I think could be useful is to port Haskell's curry and > uncurry. This is basically a convenience method for (un)wrapping an > .apply on a function object: > > function curry(f) { >return function () { >// first convert arguments to a regular array >var args = Array.prototype.slice.call(arguments); >return f(args); >} > } > > function uncurry(f) { >return function (args) { >return f.apply(this, args); >} > } > > Example use: > test = [ [10, 1], > [20, 2], > [30, 3] ]; > > assertEqual(map(uncurry(operator.plus), test), [11, 22, 33]); > > // assume join is a function that takes a list and returns a string > // with the elements joined with some delimiter > > f = curry(partial(join, _, ", ")) > assert(f("Bond", "James Bond") == "Bond, James Bond") > > Does anyone else think this could be useful? What module would it fit? > Base already has a lot of functional stuff (compose, partial, map & > friends) - I'm wondering if it fits there or if all the functional > stuff should be in a seperate module MochiKit.Functional - as Python > seems to be heading. > > cheers, > Arnar > > > > --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---
[mochikit] Re: Introducing the MochiKit.Text module
On Thu, Dec 18, 2008 at 6:15 AM, Amit Mendapara wrote: > Again, I'm too, agree with Arner. Try to follow Python conventions as > much as possible. > > startsWith => startswith > endsWith => endswith Perhaps not so surprising, but I don't agree on this. Since we are working with JavaScript here, I think we should first and foremost follow existing JavaScript conventions. Using camelCase naming is very much one of those (see the minimal built-in library and objects). Python names are not automatically the best choice for people without a strong background in Python. Some of us have other biases, being more accustomed to Java or whatever. > See `MochiKit.String.splitlines`, you can remove `splitJoin` as you > one can easily do the same with `splitlines` (if it's all about > triming newline chars). I actually removed my own stripLines to replace it with splitJoin... :-) The thing I realized, was that a stripLines() function also implied the rstripLines() and lstripLines() functions. So instead I tried to generalize it to a more generic form of map for strings. It is an experimental API for sure, but I was curious if it could be useful for more things. It is, after all, a version of map()... But this is no darling API of mine. I've registered your vote in disfavor. Any other opinions? Cheers, /Per --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to mochikit+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~--~~~~--~~--~--~---