Hello! I have a pattern of bools "0 0 1 0 0 1" and would like to take out the first ( or any other ) value and compare if is True or False (=="1"). How to achieve this?
Tried to use deconstruct to find "1" but it works only for Pattern String.
v
Hello! I have a pattern of bools "0 0 1 0 0 1" and would like to take out the first ( or any other ) value and compare if is True or False (=="1"). How to achieve this?
Tried to use deconstruct to find "1" but it works only for Pattern String.
v
Start with (== True) <$> "0 0 1 0 0 1"
and then depending on what you mean by first, you can use a struct
or segment
on it Though for Bool, "== True" is just an identity operation. So pulling out the first part of the pattern each cycle would just be:
segment 1 $ "0 0 1 0 0 1"
Hey @bgold and thank you for your reply. The comparison (== True) <$> "0 0 1 0 0 1" outputs
(0>⅙)|False
(⅙>⅓)|False
(⅓>½)|True
(½>⅔)|False
(⅔>⅚)|False
(⅚>1)|True
Being more specific I need to take the first value ( (0>⅙)|False ) and compare its bool flag in the if else statement and then do things accordingly.
Converting the Pattern Bool to array of bools would solve this for sure. I don't know if this even is doable.
// Oh I just discovered that with the show
function I can "stringify" the pattern and then check for "True". This might work in my scenario. However, I am curious if the other ways, like converting pattern to arrays are possible.
This might not be exactly what you are looking for, but the function fix
and its derivatives also allow to do some sort of if
/ else
branching. Might be worth looking into it.
If you want to ask a Pattern about events that fall within a certain time range, you can use queryArc
queryArc "0 0 1 0 0 1" (Arc 0 1)
(although note in this case since the pattern is ambiguous you'll need ("0 0 1 0 0 1" :: Pattern Bool) for the above to give meaningful output)
The list of events you get isn't guaranteed to be sorted, so if you want the earliest event, you'll need an import
import Data.List (sortOn)
sortOn (start . part) $ queryArc (rev "0 0 1 0 0 1") (Arc 0 1)
You can then do what you like with the first item of the list, though you should probably make sure the list isn't empty.