9 ms·
One of the nice things about Python is that it has syntactic sugar for this. I find [order.total for order in orders] very easy to understand in terms of
by tome 14y ago
One of the nice things about Python is that it has syntactic sugar for this. I find
[order.total for order in orders]
very easy to understand in terms of natural language. Still, now I've learned Haskell I'd prefer to write
map (^.total) orders
- adambard 14y agoAre we having a code example party? I like Clojure: (map :total orders)
- tome 14y agoYeah you're welcome to bring Clojure to my party. I've never met him/her before but he/she looks like other languages I get on well with.
- bcoates 14y agoI'm partial to select total from orders myself. I really wish was as much progress on syntaxes for expressing relational algebra as there is for the functional stuff. Dealing with map and apply seems a bit fiddly and low-level for what should be a straightforward thing to express declaratively.
- adambard 14y ago(defmacro select [total _ coll] (let [total (keyword total)] `(map ~total ~coll))) (select total from orders) Problem solved!
- draegtun 14y agoNice. Here are some interesting non-macro variants, first in Rebol: select: func [block] [ totals: [] parse block [ set this word! 'from set coll word! (totals: map-each n (get coll) [get in n this]) ] totals ] ; then later... select [total from orders] ;; nb. `select` is a core function provided by Rebol ;; so remember this example overwrites that ;; (in this scope/context) :) ;; ;; Alternative `dialect` to strive for would be.. ;; ;; doMap [select total from orders] And also in Io: select := method( msg := call argAt(0) this := msg name coll := msg next next name newMsg := message(COLL map) // COLL just a placeholder message newMsg setName(coll) // Now been changed to correct var provided newMsg next appendArg(this asMessage) call sender doMessage(newMsg) ) # then later... select(total from orders) Both examples work at runtime. However in Io you can also amend the AST directly.
- tome 14y agoI agree, but I think that `map` is already a declarative interface (albeit to an implementation that is presumably functional).
- njs12345 14y agoThere's a GHC (Haskell) extension which lets you use list comprehensions which read like SQL: http://www.haskell.org/ghc/docs/7.2.1/html/users_guide/syntax-extns.html#7.3.9.%20Generalised%20(SQL-Like)%20List%20Comprehensions http://www.haskell.org/ghc/docs/7.2.1/html/users_guide/synta...
- jimbokun 14y agoFrankly, I would like to have more code example parties. Too often, differences between programming languages are treated as strictly philosophical arguments. Comparing and contrasting actual code is far more interesting, in my opinion.
- damncabbage 14y agoParty time with Ruby: orders.map(&:total) ... Where :total is a symbol, and & is asking :total for the Proc version of itself (a Proc being an anonymous function of sorts), and map is then calling that Proc with a single argument (an order from the list of orders). More detail: http://stackoverflow.com/a/1217114/3528 http://stackoverflow.com/a/1217114/3528
- draegtun 14y agoThese are also nice and would like to join in the party :) map *.total, @orders; # perl6 @orders.map: *.total; # perl6 OO map {$_->total} @orders; # perl5 orders map(total) # Io map-each n orders [n/total] ; Rebol
- ricardobeat 14y agoNo need to go that far, CoffeeScript can do both: totals = (order.total for order in orders) totals = orders.map (o) -> o.total
- jimmaswell 14y agoThe c# syntax, myNewList = (from i in myList where i > 3 && i != 7 select i * 4).toList(), is nice too. Similar to using LINQ, myList.Where(i => i > 3), but I don't believe that form allows an easy use of the i * 4 part. One annoyance I have with .NET is there are too many types that are similar-but-not-quite a List<>, making me do conversions often. At least it's usually not much more than a .ToList(), except in the case of the controls in a Windows Form, which are their own weird list-type structure that doesn't support that.
- dbattaglia 14y agoFor older .Net collections you can use Cast<T>() to get LINQ goodness. Something like this would work: var values = from x in combobox.Items.Cast<ComboBoxItem>() where x.Value > 5 select x.Value;