Stupid Languages
nedbatchelder.com
nedbatchelder.com
The problem is parseInt, not map. Consider:
// using lodash.js (http://lodash.com/)
_.mixin({ args: function () { return _.toArray(arguments); } })
['10','10','10','10'].map(_.compose(parseInt, _.first, _.args)); // #=> [10,10,10,10] ['10','10','10','10'].map(function(n){ return parseInt(n, 10) })
That's one reason to like the conciseness of CoffeeScript, takes the urge to build compositional towers away: ['10','10','10','10'].map (n) -> parseInt(n, 10)
Which one would you rather find in your codebase?EDIT: Also, you raise a really good point in your example which is that when using parseInt you should always specify the base. If I were to modify my example to take that into account it'd get more messy.
['10','10','10','10'].map(applyRight(null, parseInt, [10], 1)
The signature is [context, fn, args, slice] where `slice` is the length of arguments to keep from invocation. It's very rare that something warrants using it vs an anonymous function though. function(arg, i){ return i%2 }
as the `slice` parameter, but that's going into crazy territory. # Adapted from http://autotelicum.github.com/Smooth-CoffeeScript/literate/partial.html
F.partial = (func, a...) -> (b...) ->
func (for arg in a then arg ?= b.shift())..., b...
For example, do `parseInt10 = F.partial(parseInt, undefined, 10)`, then `[].map(parseInt10)` does the right thing.But for the sake of argument, I'd rather see
['10','10','10','10'].map(Number);this is one is correct
['10','10','10','10'].map(String);
this one makes sense
['10','10','10','10'].map(Object);
i'd like somebody to explain me this one ?????????
Javascript is really difficult as soon as one try to do non trivial stuffs.
typeof Number("10")
//=> "number"
typeof new Number("10")
//=> "object"
The same applies to other wrapper objects, that is, Boolean and String.Lisp has some rather elegant solutions to both of those use cases. It's misleading to say that Javascript's 'map' is more powerful just because it tries to cram two or three distinct use cases into one.
It's not like Javascript even uses the Lisp definition of 'map' (which is not what most languages use for map - they use Lisp's 'mapcar').
But, JavaScript is rather functional. I've heard it described as "lisp in C's clothing". Douglas Crockford uses monads in JS (intentionally) and gave an interesting yet hard to follow introduction to JS monads not too long ago. I'd like to see the functional side of JS emphasized in the next iteration.
https://developer.mozilla.org/en-US/docs/JavaScript/Referenc...
Merely having anonymous functions does not make a language "functional". JavaScript does not encourage the use of pure functions. It does not encourage the use of recursion. It does not encourage immutability. It does not have a robust, sensible type system. It does not encourage currying.
It's pretty clear that JavaScript is inherently not a functional programming language. It goes against functional programming techniques in almost all respects, especially when it comes to JavaScript code that's out in the wild.
Neither does Lisp.
"It does not encourage the use of recursion."
Neither does Lisp. (Common Lisp has no tail recursion in the spec.)
"It does not encourage immutability."
Neither does Lisp. (Actually, ES5 does have pretty powerful primitives for immutability, Object.freeze for example.)
"It does not have a robust, sensible type system."
If you mean static typing, neither does Lisp.
"It does not encourage currying."
Neither does Lisp.
Maybe if he called his thing a Jonad, and explained how it was inspired by the monad's "executable semicolon" aspect, it would have been better.
>I've heard it described as "lisp in C's clothing"
I hear this line all the time, and it always irritates me. Saying that Javascript is Lisp-like just because it's somewhat functional is like saying that it's like Java because it's "object-oriented", even though there are a number of key differences (the use of "this", the prototypical inheritance model, runtime checks vs. static checks with late binding, etc.) It's worse, because it's rather trivial to force Javascript to behave somewhat like Java, but there's no way to force Javascript to exemplify the defining characteristics of a Lisp[0].
The nature of Lisp has nothing to do with lambdas and closures, mapcars and reduces - the phrase 'Lisp in C's clothing' doesn't even make sense, because the nature of Lisp cannot exist without an s-expression-based grammar[1]. Even though Lisp is known as a functional language, the defining characteristics of Lisp have nothing to do with it being functional.
[0] Perhaps a better analogy would be comparing it to JVM languages on the basis of the grammar, even though the two are completely orthogonal - Javascript isn't intended to run on the JVM, and while you can cross-translate code between Javascript and JVM languages and fake compatibility this way, that ability has very little do do with the defining characteristics of Javascript as a language.
[1] That doesn't mean you need to have parentheses; the grammar simply needs to be homomorphic with s-expressions, which leaves a great deal of flexibility. Javascript, however, does not make the cut.
And whether it's a stupid library is even debatable. map() has gained a more popular understanding from functional programming these days but it was probably not so when this Javascript map() function was added.
A fold/reduce is just as sometimes-reversible, when paired with an appropriate unfold. (example: multiplication and factorization)
I think you mean that map is self-composable, in the sense that the map function distributes over function-composition, in the same way that (in first-order functions) multiplication distributes over addition.
The function (constantly 0) is implemented as a place in order to maintain reversibility semantics:
((constantly 0) 10) ;-> (0 10)
((constantly 0) 50) ;-> (0 50)
The information lost by calling the constant function is pushed to the end of the list so that it can be called reversibly. This can be applied on an entire list: (map (constantly 0) [1 2 3]) ;-> ([0 0 0] [1 2 3])
> A fold/reduce is just as sometimes-reversibleThe reduce function is not reversible, but the reductions function when called with a group is:
(reductions + [1 2 3 4]) ;-> [1 3 6 10]
The reduce value is the last element of the list so it can be retrieved by the last place: (last [1 3 6 10]) ;-> (10 [1 3 6])
> I think you mean that map is self-composableThe preservation of information is more important to me then composition.
In Python for example you can explicitly declare variadic functions to ignore extra arguments. But when it's the default behavior you can for example change the official signature of a callback interface without breaking compatibility. It can be pretty liberating to work in a best-effort language...though perhaps it makes testing a bit more important.
The array-like `arguments` object will always reflect what was actually passed to the function though.
My knowledge of Python is limited, but I was under the impression that it is call by value just like everything else (where Python's notion of value is "pointer to something"). Am I missing something?
Names refer to objects. Names are introduced by name binding operations such as `=`, `import`, `def`.
def f(a, b): # a, b - local variables
a = [1] # doesn't change x list; a refers to the new list hence forth
b.append(2) # change y list; b refers to the same list as y
x, y = [], []
f(x, y)
print x, y # [], [2]
Note: a "win" doesn't change the truth. It just means that nobody cared enough to participate further in the hostile discussion.Similarly, "pointer to something" is not a useful way to think about Python, because people with a background in C will start making assumptions about what "pointer" means. You might think that assigning to that "pointer" changes what's stored in the memory it points to, but really all it does is re-bind a name within the (local, which does not affect global) scope, leaving the "memory" unchanged.
So rather than ask which misleading-analogy-to-another-language Python uses, just ask how Python works.
Since this is exactly the behaviour of Python's function calls, how is describing Python as call by value "not useful"? And where's this "misleading analogy to another language", given the concept is clearly explained without reference to any particular language?
I'm prepared to accept that people become confused over this, but I reject the notion that it is not a useful question.
Except this isn't useful, because Python doesn't work the way those other languages do. They'll run into cases where their mental model of what "pass-by-value" or "pass-by-reference" means does not match up to Python's actual workings, and then they'll spend endless time arguing about which model Python uses when the answer is really "neither, stop trying to shoehorn one of those models onto a language that doesn't fit".
var publishedPosts = posts.filter(function(post){
return post.published
})
var intervals = publishedPosts.map(function(post, i){
var next = publishedPosts[i+1]
return post.date - (next && next.date || 0)
})
With the array reference you don't need an intermediate var: var intervals = posts.filter(function(post){
return post.published
}).map(function(post, i, arr){
var next = arr[i+1]
return post.date - (next && next.date || 0)
})0 = 0
00 = 1
000 = 2
0000 = 3
etc
If a base means, you can write numbers of this form:
abc
Where the value is:
aB^2 + bB^1 + cB^0
and you can only have B symbols, then your one symbol for base 1 would need to be 1, still useless, but not completely:
1 = 11^0 = 1 11 = 11^1 + 11^0 = 2 ...
Note that you cannot represent 0 in base 1 with this method.
If you choose 0 as your one symbol for base 1, then the only number you can represent is 0. I assert this is even more useless than selecting 1 as the symbol.
0 = 01^0 = 0 00 = 01^1 + 01^0 = 0 ...
As far as I can tell, the example was trying to interpret "10" as base 1. 10 has two different symbols which truly does not make sense in base 1.
I assert that base 1 is not a valid base, because you cannot represent all integers in it.
Additionally, the "unary point" or whatever it would be called, would serve no purpose:
1.1 = 11^0 + 1*1^-1 = 2
I'm not sure how that might disqualify something for a "base" but it certainly doesn't help :) Probably the strongest argument is the inability to represent 0.
https://en.wikipedia.org/wiki/Base_1
though you are right that it's not the same as base N, N>1. Also, Wikipedia uses |||| to represent 4 instead of 0000. It makes more sense.
: 0
o : 1
oo : 2
ooo: 3
Of course, this is not a practical notation, since 0 would hard to distinguish from actual absence, but it would be viable in some situations: a table of numbers say, where you know that no cells are empty. So theoretically, it is possible to represent 0 in base 1, regardless of its obvious impracticalities.If we were treating it like base 2 (or 10 or 8 or 16), we would evaluate 0000 as 1^30+1^20+1^10+1^00 = 0. A number system where the only number you can represent is 0 is a bit less useful.