Skip to content
forked from robrix/Prelude

Swift µframework of simple functional programming tools

License

Notifications You must be signed in to change notification settings

carabina/Prelude

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Prelude

This is a Swift microframework providing a number of simple functions that I use in many of my other frameworks. Rather than continue to reimplement them for each consumer, I am gathering them here together.

Notably, this framework does not provide any new types, or any functions which operate on custom types; those presumably belong in µframeworks of their own.

Use

id

Passing id as the argument to the flattenMap method of a Stream of Streams will flatten it out into a stream of all the nested elements:

func flatten<T>(stream: Stream<Stream<T>>) -> Stream<T> {
	return stream.flattenMap(id)
}

const

Passing the result of const to an Either is convenient for transforming it into an Optional<T>:

let result: Either<NSError, String> = 
if let string = result.either(const(nil), id) {
	println("ohai \($0)")
}

>>> and <<<

The left-to-right and right-to-left composition operators (>>> and <<< respectively) chain operations together:

let repl: File -> String = readLine >>> parseString >>> evaluateAST >>> toString
while true {
	println(repl(standardInput))
}

fix

You can use fix to make an anonymous function which calls itself recursively:

let factorial = fix { recur in
	{ n in n > 0 ? n * recur(n - 1) : 1 }
}

Documentation

API documentation is in the source.

Integration

  1. Add this repository as a submodule and check out its dependencies, and/or add it to your Cartfile if you’re using carthage to manage your dependencies.
  2. Drag Prelude.xcodeproj into your project or workspace.
  3. Link your target against Prelude.framework and each of the dependency frameworks.
  4. Application targets should ensure that the framework gets copied into their application bundle. (Framework targets should instead require the application linking them to include Prelude.)

About

Swift µframework of simple functional programming tools

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 96.9%
  • C++ 3.1%