Skip to content
/ Memo Public
forked from robrix/Memo

Swift µframework of Memo, a lazily memoized value.

License

Notifications You must be signed in to change notification settings

335g/Memo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

74 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memo

This is a Swift microframework providing Memo<T>, with implementations of ==/!= where T: Equatable.

Memo is a convenient way to ensure that an expression producing a value is evaluated precisely once. It can be used to lazily load expensive resources or to delay and memoize recursive evaluation to avoid infinite loops.

Use

Constructing a Memo is easy. Most of the time you just want to lazily memoize an expression, so you’ll want to use the default, unlabelled initializer:

var memos = 0
let closure = Memo { ++memos }

Sometimes you’ll want to pass in a function () -> T. In those cases, use the unevaluated initializer:

let preIncrementMemos = { ++memos }
let lazilyEvaluated = Memo(unevaluated: preIncrementMemos)

Sometimes you’ve already evaluated, or you want to force the evaluation to happen with the construction of the Memo for performance reasons. The evaluated initializer handles this:

let eagerlyEvaluated = Memo(evaluated: ++memos)

Extract the evaluated value via the value property:

let value = implicitClosure.value

Implementation details

Memo is memoized even if you copy it around before it’s evaluated. It uses immutable value type semantics, but does not leak implementation details (i.e. you do not have to hold it in a var to retrieve its value). It does this by means of a private, unobservably-mutable reference type which wraps the inner data.

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 Memo.xcodeproj into your project or workspace, and do the same with its dependencies (i.e. the other .xcodeproj files included in Memo.xcworkspace). NB: Memo.xcworkspace is for standalone development of Memo, while Memo.xcodeproj is for targets using Memo as a dependency.
  3. Link your target against Memo.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 Memo and its dependencies.)

About

Swift µframework of Memo, a lazily memoized value.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 95.4%
  • C 4.6%