Elm function with type: Signal (list a) & # 8594; List (signal a)

I am new to knitting and functional programming in general. But I use elm, and I really need a function that has Signal (List String) as input and returns List (Signal String).

I know that I probably should not have this problem with the best architectural design in my program, but having a function that could do this would solve a big problem for me.

The combined function does the opposite:

combine : List (Signal a) -> Signal (List a) combine = List.foldr (map2 (::)) (constant []) 

I tried to do something similar to the function of a combine, but so far have not been successful. Any ideas on how to create such a function?

+6
source share
1 answer

It is impossible at all

Reverse to combine not (generally) possible.
When you have a list of static signal sizes, you can combine them into a list of static size lists. But when you go the other way, there is no guarantee that the lists in the signal are static in size. Therefore, you cannot simply create a list from it.
(If you could, then the normal value of the List type could have a variable size without showing Signal around the type, and you would dynamically create and destroy signals in the list. These are two things that prohibit Elm.)

But with some limitations ...

Of course, if you know that the list in the signal is static in size, you can write a specific function based on this assumption; this function will then fail at runtime if there is a case where your assumption of static size lists was wrong.

 unsafe : Maybe a -> a unsafeHead m = case m of Just a -> a Nothing -> Debug.crash "unsafe: You're out of luck. The `Maybe` was not a `Just`. " uncombine : Int -> Signal (List a) -> List (Signal a) uncombine n sig = if n == 0 then [] else Signal.map (List.head >> unsafe) sig :: uncombine (n-1) (Signal.map (List.tail >> unsafe) sig) 

(I am pretty sure this issue was discussed on the elm-discuss mailing list once, but I can no longer find it)

+4
source

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


All Articles