Discussion:
find if all items in an array are nil
enrike
2014-09-23 09:47:21 UTC
Permalink
hi

is there a simple way to check if all items in an array are nil?

I am currently using a function to loop the array and check one by one.

thanks

enrike

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Daniel Mayer
2014-09-23 10:03:29 UTC
Permalink
Post by enrike
hi
is there a simple way to check if all items in an array are nil?
[1, nil].every(_.isNil)


Greetings

Daniel

-----------------------------
www.daniel-mayer.at
-----------------------------



_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
enrike
2014-09-23 10:07:44 UTC
Permalink
thanks!! I thought there was a simple way but I could not find it!
Post by Daniel Mayer
[1, nil].every(_.isNil)
_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/
Miguel Negrão
2014-09-23 10:28:51 UTC
Permalink
Post by enrike
hi
is there a simple way to check if all items in an array are nil?
I am currently using a function to loop the array and check one by one.
thanks
enrike
Hi

This should work:

[nil,nil,nil].collect(_.isNil).reduce('&&') -> true
[nil,nil,2].collect(_.isNil).reduce('&&') -> false

This is an example of the general operation of 'folding'
(https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29). the
'reduce' method is a special case where the binary function of the fold
takes both arguments from the same kind (the kind of the elements in the
array) and expects an array of at least 1 element. Also, if the type of
elements in the array is a monoid for some binary function, then you can
just collapse the array to a single value without explicitelly
specifying[1] the binary function or the start element[2].

best,
Miguel

[1] unless the type has many possible monoid structures, which is the
case of boolean which can be a monoid for 'and', 'or' and 'xor'. In that
case you have to somehow indicate which operation you want to use.
[2]
http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Monoid.html#v:mconcat
https://github.com/miguel-negrao/FPLib/blob/master/Classes/Additions/TypeClasses-Object.sc#L134
enrike
2014-09-23 11:38:41 UTC
Permalink
ok, thanks for the explanation!
Post by Miguel Negrão
Post by enrike
hi
is there a simple way to check if all items in an array are nil?
I am currently using a function to loop the array and check one by one.
thanks
enrike
Hi
[nil,nil,nil].collect(_.isNil).reduce('&&') -> true
[nil,nil,2].collect(_.isNil).reduce('&&') -> false
This is an example of the general operation of 'folding'
(https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29). the
'reduce' method is a special case where the binary function of the fold
takes both arguments from the same kind (the kind of the elements in the
array) and expects an array of at least 1 element. Also, if the type of
elements in the array is a monoid for some binary function, then you can
just collapse the array to a single value without explicitelly
specifying[1] the binary function or the start element[2].
best,
Miguel
[1] unless the type has many possible monoid structures, which is the
case of boolean which can be a monoid for 'and', 'or' and 'xor'. In that
case you have to somehow indicate which operation you want to use.
[2]
http://hackage.haskell.org/package/base-4.7.0.1/docs/Data-Monoid.html#v:mconcat
https://github.com/miguel-negrao/FPLib/blob/master/Classes/Additions/TypeClasses-Object.sc#L134
_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: http://www.listarc.bham.ac.uk/marchives/sc-users/
search: http://www.listarc.bham.ac.uk/lists/sc-users/search/

Loading...