# Piping a value throught a list of functions of unknown length

**URL:** <https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277>\
**Category:** Learn\
**Created:** [October 19, 2018, 7:24am UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277 "2018-10-19T07:24:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![caw](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@caw](https://discourse.elm-lang.org/u/caw)\
**Post date:** [October 19, 2018, 7:24am UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/1 "2018-10-19T07:24:35Z")

</div>

I would like to be able to pipe a value through a list of functions of unknown length:

```auto
lst = [\x ->x + 1, \x -> x + 2]
someFunc 2 lst 

```

would give 5

This works

[https://ellie-app.com/3FjhGztpXmqa1](https://ellie-app.com/3FjhGztpXmqa1)

but I can’t help wondering if there’s a “neater” way? I wonder if I knew more about applicative functors I might be able to see a better way?

thoughts on how to do this in a neater way would be appreciated1

thanks

```auto
pipeThroughListOfFunctions : value -> List (value -> value) -> value
pipeThroughListOfFunctions value list =
    case list of
        [] ->
            value

        [fn] ->
            fn value

        h :: t ->
            pipeThroughListOfFunctions (h value) t

```

---

<div class="post-metadata">

**Author:** ![gampleman](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/gampleman/32/43_2.png) [@gampleman](https://discourse.elm-lang.org/u/gampleman)\
**Post date:** [October 19, 2018, 7:37am UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/2 "2018-10-19T07:37:30Z")

</div>

You can do `List.foldl (<|) 2 lst`.

This is equivalent to the `pipeThroughListOfFunctions` you wrote, since you can think of folds as an abstraction over recursion and the `<|` operator simply expresses function application.

---

<div class="post-metadata">

**Author:** ![caw](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@caw](https://discourse.elm-lang.org/u/caw)\
**Post date:** [October 19, 2018, 12:15pm UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/3 "2018-10-19T12:15:38Z")

</div>

Thanks very much @gameplan - that’s exactly the elegant kind of solution I thought would be possible  
This functional programming is really elegant - mine looks like Scheme, yours looks much nicer!  
again, thanks.

---

<div class="post-metadata">

**Author:** ![andys8](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/andys8/32/280_2.png) [@andys8](https://discourse.elm-lang.org/u/andys8)\
**Post date:** [October 20, 2018, 12:26pm UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/4 "2018-10-20T12:26:09Z")

</div>

An alternative would be to fold the list with the [`compose` operator](https://package.elm-lang.org/packages/elm/core/latest/Basics#(%3E%3E)) (`>>` or `<<`). This way the result is a new function that is the sequential combination of all in the list.

```elm
lst =
    [\x -> x + 1, \x -> x * 2]

composedFunction =
    List.foldl (<<) identity lst

composedFunction 2

```

This is an alternative to the approach which is applying the value directly with the pipe operator (`|>`). It can make sense if several lists will be composed, or passed around. It could also be more clear with good naming (What will happen if all the functions are composed?). Otherwise @gampleman mentioned the most concise solution for this problem. Perfectly fine.

---

<div class="post-metadata">

**Author:** ![caw](https://avatars.discourse-cdn.com/v4/letter/c/e5b9ba/32.png) [@caw](https://discourse.elm-lang.org/u/caw)\
**Post date:** [October 21, 2018, 12:56am UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/5 "2018-10-21T00:56:45Z")

</div>

Ah yes - thanks @andys8 - I’ll work piping vs composing…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 31, 2018, 12:56am UTC](https://discourse.elm-lang.org/t/piping-a-value-throught-a-list-of-functions-of-unknown-length/2277/6 "2018-10-31T00:56:46Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
