arliss_obsidian/fp/Language/Functions/Composition.md
Orion Kindel 3701b1e2f8
update
2024-09-23 18:56:55 -05:00

1.1 KiB

The most common pattern by far; piping the output of a Functions into the input of the next.

compose :: forall a b c. (b -> c) -> (a -> b) -> (a -> c)
compose g f a = g (f a)

compose (infix as <<<) accepts 2 functions; f which is a -> b and g; b -> c. compose returns a new function a -> c that "glues" the 2 functions together; piping the output of f into g.

e.g.

consider a function normalizedPathSegments :: String -> Array String

This function would normalize a file path, removing trailing / leading slashes and resolving relative paths, then split the path by its segments.

A very good approach would be to split this function into separate single-purpose components, e.g.

  • stripLeadingSlash :: String -> String
  • stripTrailingSlash :: String -> String
  • splitPath :: String -> Array String
  • normalizePathSegments :: Array String -> Array String

then define normalizedPathSegments like so:

normalizedPathSegments :: String -> Array String
normalizedPathSegments =
  normalizePathSegments
  <<< splitPath
  <<< stripTrailingSlash
  <<< stripLeadingSlash