3 ms·
I like the Python version of: def square(vector): return [v**2 for v in vector]
by milliams 13y ago
I like the Python version of:
def square(vector):
return [v**2 for v in vector]
- ptr 13y agoHaskell: let square = map (^ 2) Real partial application and currying is something I'm longing for in other languages; it's easy to fall for the beauty of the Point Free Style.
- icebraining 13y agoWell, in Python that would be: from functools import partial square = partial(map, lambda i: i**2) EDIT: I guess it's still not the same, since the lambda isn't a partial application. But I don't think we can just set the exponent in pow(), we need to create a function that takes the arguments in the reverse order. Maybe: from functools import wraps, partial def flip(func): 'Create a new function from the original with the arguments reversed' @wraps(func) def newfunc(*args): return func(*args[::-1]) return newfunc _pow = flip(pow) #Take Y, then X # Then the equivalent would be square = partial(map, partial(_pow, 2) Of course, flip is rather long-winded because it's a generic function, one could just do: _pow = lambda y, x: pow(x, y)
- deleted 13y ago[deleted]
- cousin_it 13y agoOr SQL: SELECT x*x FROM vector; There's just so many things that SQL got right: - Non-programmers actually read and modify SQL code. I swear, it happens. It doesn't happen very much even with Python, and it will never happen with C++. - It can join multiple data structures in a single query, on arbitrary criteria. - A query can be optimized as a whole. Optimizers freely reorder the parts and actually come up with good plans. - The presence or absence of indexes (data structures used for acceleration) is transparent to the query. Can any other language do that? A lot of the code I've seen would be 10x to 100x smaller if expressed in SQL, or an SQL-like language that can handle nested data structures (which is something that I really want).
- enricosada 13y agof# multiple implementations (comprension, partial, parallel) //array comprehension let squareVec vector = [| for x in vector -> x ** 2.0 |] squareVec [| 1.0; 2.0; 3.0 |] [| 1.0; 2.0; 3.0 |] |> squareVec //inline function [| 1;2;3 |] |> Array.map (fun x -> x * x) //extract function let square x = x * x [| 1;2;3 |] |> Array.map square //partial let squareVec3 = Array.map square [| 1;2;3 |] |> squareVec3 //partial let pow x y = y ** x [| 1.0; 2.0; 3.0 |] |> Array.map (pow 2.0) //parallel [| 1.0; 2.0; 3.0 |] |> Array.Parallel.map (pow 2.0) //can use SIMD on mono (next version on .NET too) //no first hand experience, but can also use GPU from F#