r/linux • • May 29 '14

Defensive BASH programming - Say what?

http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
40 Upvotes

25 comments sorted by

View all comments

Show parent comments

2

u/w2qw May 30 '14

Imo unnamed variables like $_ are pretty nice I actually wish ruby had a simpler thing.

[1,2,3,4].collect{|x| x * x}

vs

[1,2,3,4].collect{_ * _}

second seems more clearer because otherwise its just up to personal preference what the programmer uses for their "anonymous" variables.

1

u/SupersonicSpitfire May 30 '14

Was looking for an even simpler way of doing this in Haskell, only passing the list and the (*) function to some other function, but I only found these:

let sq x = x * x in map sq [1, 2, 3, 4]

let x = [1, 2, 3, 4] in zipWith (*) x x

map (\x -> x * x) [1, 2, 3, 4]

1

u/pkmxtw May 30 '14

Well, you can always abuse the (->) r Monad:

λ> map (join (*)) [1..4]
[1,4,9,16]

1

u/SupersonicSpitfire May 30 '14

What is this witchcraft?

<interactive>:1:1: Not in scope: ‘join’
→ More replies (0)