TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Making Map Operations Implicit in Programming

14 pointsby farginayover 11 years ago

12 comments

jherikoover 11 years ago
This is an &#x27;ancient&#x27; idea pre-dating the recent functional craze: array programming, it is so well known that it has a substantial wikipedia page:<p><a href="http://en.wikipedia.org/wiki/Array_programming" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Array_programming</a><p>its also important to note that this is syntactic sugar - and like most kinds of syntactic sugar it can make code confusing to read, in order to make it prettier. being explicit is often a very good thing for readability and for compilers (they must also read your code) - i value it highly.<p>although there is something nice about being able to add two arrays together and it do &#x27;what i would expect&#x27;... but what do i expect if the arrays do not match in size? how much more thinking do i need to do over being explicit?
评论 #6922002 未加载
latkover 11 years ago
This is a standard feature of many languages or libraries, and generally known as <i>array programming</i> – something APL is known for. It crops up in numerical libraries like &quot;numpy&quot; (Python) or &quot;PDL&quot; (Perl) where selected scalar operations distribute over the whole collection. This is great when using collections of numbers, but is not very generalizable: It may not be possible to distinguish whether a method was called on a collection or all items in the collection. One solution would be a “mapping method call operator” like &quot;!&quot; (it&#x27;s like a method call &quot;.&quot; <i>and</i> a shell pipe &quot;|&quot;) – but now we have an explicit &quot;map&quot; again.<p>I think an explicit &quot;map&quot; is more flexible in a general-purpose language. Some languages do interesting experiments with this, e.g. Perl6 which uses “hyperoperators” to precisely control how a given operator is applied.
DanWaterworthover 11 years ago
How many times will Monads be reinvented?<p><pre><code> name.trim.filter{ _.length != 0 }.toUpperCase </code></pre> becomes:<p><pre><code> name &gt;&gt;= (return . trim) &gt;&gt;= (guard . ((&#x2F;= 0) . length)) &gt;&gt;= (return . toUpperCase) </code></pre> I&#x27;ve purposefully used &gt;&gt;= and return instead of fmap to show that &#x27;.&#x27; above is almost &gt;&gt;=.<p>EDIT: I&#x27;ve just noticed a type error in the use of guard, but you get the idea.
评论 #6921743 未加载
dragonwriterover 11 years ago
The problem I see is that implicit mapping restricts the scope of operations that can work on collections -- if there is no distinction between applying an operation to an single object and applying the same operation to all members of a collection, then no collection can directly support the same operation as anything which might be a member of the collection, because you can&#x27;t disambiguate between attempts to operate on the collection and attempts to operate on the members.<p>This would seem to be a problem in general, and a particular problem for nested collections. Explicitness makes intent clear.
评论 #6929055 未加载
axblountover 11 years ago
The example in the article was taken directly from the Scala documentation for [Option](<a href="http://www.scala-lang.org/api/current/index.html#scala.Option" rel="nofollow">http:&#x2F;&#x2F;www.scala-lang.org&#x2F;api&#x2F;current&#x2F;index.html#scala.Optio...</a>). The map <i>is</i> implicit if you use Scala&#x27;s for notation. From the linked docs:<p><pre><code> val upper = for { name &lt;- request getParameter &quot;name&quot; trimmed &lt;- Some(name.trim) upper &lt;- Some(trimmed.toUpperCase) if trimmed.length != 0 } yield upper println(upper getOrElse &quot;&quot;) </code></pre> In Scala, an implementation of flatMap (bind), map, and filter allows you to use for expressions like the above. Objects that support these operations are not strictly monads, but they&#x27;re close enough for most practical uses. Haskell is great, but give Scala a little credit.
评论 #6922192 未加载
dangirshover 11 years ago
This is somewhat similar to the List monad in Haskell:<p><a href="http://en.m.wikibooks.org/wiki/Haskell/Understanding_monads/List" rel="nofollow">http:&#x2F;&#x2F;en.m.wikibooks.org&#x2F;wiki&#x2F;Haskell&#x2F;Understanding_monads&#x2F;...</a><p>Bind is concatMap and return puts something into a singleton list. This allows &quot;nondeterministic&quot; computations, where applying functions over all elements in the list is implicit (via bind). Using the monad is not the same as having map be implicit everywhere, but it allows cases where you need several chained implicit (concat)maps to be cleanly implemented.<p>This is arguably a better design than making all functions polymorphic over lists, and having implicit maps be a global rule (rather than something constrained in a monad&#x27;s context).
hopp_checkover 11 years ago
How difficult will code be to read if implicit operations are happening without any mention of the language construct executing them? As far as this goes, Haskell&#x27;s Maybe monad is the simplest system I&#x27;ve ever seen:<p><pre><code> let x = Just 3 in do value &lt;- x guard (value &gt; 1) return (value * 4) </code></pre> If x was &#x27;Nothing&#x27; then the whole operation would short circuit and &#x27;Nothing&#x27; would be returned. If the guard fails, then &#x27;Nothing&#x27; is returned.
porgesover 11 years ago
My favourite example of a language with &#x27;automatic functorization&#x27; is Icon&#x2F;Unicon. All functions, when applied to generators, themselves become generators.<p>So e.g. `read` is a generator that reads lines from the input, `write` writes a line to output, and the composition of them, `write(read())`, is a generator that copies lines from input to output. To run the generator, you can write `while write(read())`.
rukednousover 11 years ago
&gt; What would our languages look like if we made them shape polymorphic? If we moved in that direction it would be like baking the Composite Design Pattern deeply into language machinery. It could be very powerful.<p>In addition to APL and J, that would look a lot like SQL as well.
评论 #6921862 未加载
ygraover 11 years ago
R also doesn&#x27;t distinguish between applying an operation to a scalar or a vector.
farginayover 11 years ago
Erik Meijer points to this as research in this area: <a href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.146.5411" rel="nofollow">http:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;viewdoc&#x2F;summary?doi=10.1.1.146....</a>
rianover 11 years ago
This is already the case in Haskell. Look up functor
评论 #6921650 未加载