Is concurrent function declaration a good idea?

Answering this question , I realized that Haskell allows you to deconstruct an object when declaring a function and use it to define parallel functions.

Basically, imagine that I would like to do something like this

  a = (1+) b = (2+) c = (3+) d = (4+) e = (5+) 

What will be the DRYest way to write it? I mean, all the templates are the same: the function name cannot be guessed by the amount to be added, but I should be able to avoid writing + every time (without using the Haskell template, of course).

Basically, I would like to be able to use map or something similar, and it seems that map just works !.

 [a,b,c,d,e] = map (+) [1..5] 

Et voila!

This is much shorter and perhaps more expressive, and it works:

 > a 10 11 > b 10 12 etc ... 

So my question is: is it good to use (in such a situation), and if not, is there a downside (I know that buying experience is equivalent in Ruby ) is a nightmare, but the reasons do not seem to apply to Haskell)?

Update

I understand that the readability of such code is subjective and can be considered as based on opinions. However, there may be some objective reasons for not doing this. For example, in Ruby (at least 1.8), methods defined with define_method are invisible to most IDE and tag developers. Moreover, you cannot enter them using a debugger, etc., which makes them really inconvenient in practice. I ask for a similar reason in Haskell

+6
source share
2 answers

One flaw with

 [a,b,c,d,e] = map (+) [1..5] 

lies in the fact that this requires a non-exhaustive comparison with the sample - the match is not guaranteed statically. Obviously, in this case there is no problem, but in a more subtle example, where perhaps [1..5] was defined elsewhere, it may be more difficult to see.

I also do not see any particular advantages for this style in this particular case, although I understand that this is a slightly contrived example.

One case when defining multiple values ​​on a single line is worth it if you have a function that returns a tuple, for example:

 (xs, ys) = unzip [(1, 'a'), (2, 'b')] 
+8
source

This can be done using the package for homogeneous tuples (which I am the author):

 import Data.Tuple.Homogenous ... let Tuple4 (a, b, c, d) = fmap (+) $ Tuple4 (1, 2, 3, 4) in ... 

without resorting to exhaustive pattern matching.

There is also another similar tup-functor package.

+6
source

Source: https://habr.com/ru/post/971824/


All Articles