Learn from Haskell - Functional, Reusable JavaScript
11–20 of 50 posts
Re: Learn from Haskell - Functional, Reusable JavaScript
#12One might as well say "Learn from Lisp" or "Learn from ML" as there really isn't anything Haskell specific about these kind of higher order functions. If we want to learn from Haskell, the big ideas are functional purity and its type system.
In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option
choice 1
choice 2
choice 3
I ended up populating the dropdown list using a bunch of javascript functions that looked like this function () { return { key: 1, value: 'choice 1' } }
function () { return { key: 2, value: 'choice 2' } }
function () { return { key: 3, value: 'choice 3' } }
function () { /* do stuff that creates a new choice */ }
Not sure I would have thought of that if I had not first played with Haskell and grokked laziness.Re: Learn from Haskell - Functional, Reusable JavaScript
#13Earlier quoted context omitted.
The map is unnecessary. Just reduce: (* assuming: val total_len : string -> int *) List.reduce seq ~f:(fun a b -> if (total_len a) > (total_len b) then a else b) It's not necessary in this case, but fold is often a lot more useful than reduce . At least the way I think of it, the type of reduce is 'a list -> ('a -> 'a -> 'a) -> 'a , whereas the type of fold is 'a list -> init:'b -> ('b -> 'a -> 'b) -> 'b . The upside…
Aren't I then computing the value of total_len for my largest object numerous times? That was why I went for the map, it made some sense to pre-compute the lengths and then find the largest one. Certainly I could implement some caching mechanism but that would come with some implementation complexity?
let precomp = List.map lst (fun el -> ((length el), el)) in
let get_max (l1, el1) (l2, el2) = el1 if l1 > l2 else el2 in
List.reduce lst get_maxRe: Learn from Haskell - Functional, Reusable JavaScript
#14One might as well say "Learn from Lisp" or "Learn from ML" as there really isn't anything Haskell specific about these kind of higher order functions. If we want to learn from Haskell, the big ideas are functional purity and its type system.
The biggest idea is probably laziness. In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option choice 1 choice…
It's simply not true. In fact, because Haskell is statically typed, it's very easy to tell what's a function and what's not: If its type doesn't have an -> in it, it's not a function.
Re: Learn from Haskell - Functional, Reusable JavaScript
#15One might as well say "Learn from Lisp" or "Learn from ML" as there really isn't anything Haskell specific about these kind of higher order functions. If we want to learn from Haskell, the big ideas are functional purity and its type system.
The biggest idea is probably laziness. In Haskell, everything is basically a function. Even the number 3 is really just a function that if and when evaluated will return the value 3. I used that concept to to simply some JavaScript UI code a while back. I had SELECT element that I needed to populate dynamically but wanted the last option to invoke an action that allowed the user to add another option choice 1 choice…
Haskell doesn't define integers as functions, they are machine integers just like most other languages. You may be thinking of the Peano numbers, which do have a particularly nice representation in Haskell but they certainly aren't used by default.
Re: Learn from Haskell - Functional, Reusable JavaScript
#16One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…
longestName = maximumBy . comparing $ \(f,l) -> length $ f++lRe: Learn from Haskell - Functional, Reusable JavaScript
#17One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…
people = [Person('foo', 'bar'), Person('John', 'Doe'), Person('Jane', 'Anonymous')]
key = lambda person: len(person.first_name + person.last_name)
max(people, key=key)Re: Learn from Haskell - Functional, Reusable JavaScript
#18Re: Learn from Haskell - Functional, Reusable JavaScript
#19One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…
The pattern you mention of using a tuple of key-value pairs is not that bad and it even has a name. The Perl people call it the Swartzian transform. An alternative if you don't like that would be having the maximum-finding function receive an additional comparator argument, similarly to qsort.
Yup, though one small thing: it's 'Schwartzian transform'[1] for Randal Schwartz.
Re: Learn from Haskell - Functional, Reusable JavaScript
#20One bit of trouble I have with functional style programming is something like the following example: if I have an array of people with firstName and lastName properties, how would I go about returning the person with the longest full name without adding any properties directly to the objects? It is a simple map then reduce to return the maximum length, a fairly trivial modification thereof to return the full name, bu…
(argmax (λ(c) (string-length (customer-name c)))
customers)
http://docs.racket-lang.org/reference/pairs.html?q=argmax#(d...)Haskell has argmaxBy: http://hackage.haskell.org/packages/archive/list-extras/0.3....
If your language doesn't have argmax, fold the list with the best value, like this in lisp:
(define (my-argmax fun lst)
(foldl (λ(prev-max elt)
(if (
No extra space usage, no temporary values, no sorting; all in O(N) time. :)