Skip to content

What about applyTo, applyTo2 and apply2AndMerge for dealing with functions? [RFC] #17

@Clindbergh

Description

@Clindbergh

I am wondering if these functions could be an useful addition or if these are already covered by other packages or if there is some syntax available I am not aware of that would make them superfluous. I find them useful especially because I can do partial application using the original functions.

  •   applyTo : a -> (a -> b) -> b
      applyTo value function =
          function value
    
    
      applyMultipleTo: a -> List (a -> b) -> List (b)
      applyMultipleToinput functions =
          map (applyTo input) functions
    

    This allows to do things like this:

      [ sqrt, pow 2, add 3 ]
      |> applyMultipleTo5
  •   applyTo2 : a -> (a -> b) -> (a -> c) -> (b, c)
      applyTo2 value function1 function2 =
          (function1 value, function2 value)
    
    
      apply2 : (a -> b) -> (a -> c) -> a -> (b, c)
      apply2 function1 function2 value =
          applyTo2 value function1 function2

    These are similar to applyToMultiple, except that we have exactly two functions and two outputs.

  •   apply2AndMerge : (a -> b) -> (a -> c) -> (b -> c -> d) -> a -> d
      apply2AndMerge function1 function2 merge value =
          merge (function1 value) (function2 value)

    This allows something like apply2AndMerge sqrt (pow 2) min which yields a function that returns the smaller value of sqrt and pow 2.
    A version applyMultipleAndMerge might also be interesting, but the function might be a bit more elaborate.

      applyMultipleAndMerge : List (a -> b) -> (b -> b -> d) -> a -> d
      applyMultipleAndMerge functions merge value =

    This would not work in the case the list is empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions