Package

purescript-eta-conversion

Repository
pujoheadsoft/purescript-eta-conversion
License
MIT
Uploaded by
pacchettibotti
Published on
2023-12-09T18:05:52Z

Libraries that support eta conversion.

Generates a new function that can be eta conversion from function.

日本語版 README

instration

Install with Spago:

spago install eta-conversion

Usage

Basics

Suppose you have the following data structure Result and function fun and a function called exampleA1 that uses them.

newtype Result a = Result a

fun :: String -> Int -> Boolean -> String
fun _ _ _ = ""

exampleA1 :: String -> Int -> Boolean -> Result String
exampleA1 s i b = Result $ fun s i b

By using the <<| operator for this exampleA1 function, the following eta conversion can be performed.

import Data.EtaConversionTransformer ((<<|))

exampleA2 :: String -> Int -> Boolean -> Result String
exampleA2 = Result <<| fun

This works the same as if you had written.

exampleA2 = \s i b -> Result $ fun s i b

That is, a <<| f creates a new function that has the same arguments as function f and returns the result of applying those arguments to f and passing it to function a.

<<| signature as follows when there is only one argument.

(o -> ret) -> (a1 -> o) -> (a1 -> ret)

If there are two arguments, signature is as follows.

(o -> ret) -> (a1 -> a2 -> o) -> (a1 -> a2 -> ret)

When there are three or more arguments, the number of arguments in the signature increases in the same pattern as above.

<<| supports up to 9 arguments.

Generate a new function with left-rotated order of argument definitions

The rotate function can be used to generate a new function with a shifted argument definition as follows.

import Data.ArgsRotater (rotate)

fun :: String -> Int -> Boolean -> String
fun _ _ _ = ""

example :: Boolean -> String -> Int -> String
example = rotate fun

The <^ operator can be used to create a new function with the last argument already applied.

import Data.ArgsRotater ((<^))

fun :: String -> Int -> Boolean -> String
fun _ _ _ = ""

example :: String -> Int -> String
example = fun <^ true -- last args applied function

Generate a 'function that generates a data structure with a function'

Suppose we have the following data structures Result,Functions, a function runFunctions to retrieve the contents of Functions, and a function exampleB1 to use them.

newtype Result a = Result a

newtype Functions = Functions {
  fun :: String -> Int -> Boolean -> String
}

runFunctions :: Functions -> { fun :: String -> Int -> Boolean -> String }
runFunctions (Functions r) = r

exampleB1 :: String -> Int -> Boolean -> Result (Functions -> String)
exampleB1 s i b = Result $ (\f -> f.fun s i b) <<< runFunctions

Using the <<: operator, this exampleB1 can be defined as follows.

import Data.EtaConversionTransformer ((<<:))

exampleB2 :: String -> Int -> Boolean -> Result (Functions -> String)
exampleB2 = Result <<: _.fun <<< runFunctions

Generate a "function to generate a ReaderT that would take a function out of the environment and use it"

Suppose we want to generate a ReaderT that takes a record with a function as its environment and uses that function internally as follows.

exampleC1 :: forall r m. TypeEquals r { fun :: String -> m Unit } => String -> ReaderT r m Unit
exampleC1 s = ReaderT $ \r -> (to r).fun s

Such a function can be written using the readerT function as follows.

import Data.ReaderTEtaConversionTransformer (readerT)

exampleC2 :: forall r m. TypeEquals r { fun :: String -> m Unit } => String -> ReaderT r m Unit
exampleC2 = readerT _.fun