Skip to content

Commit

Permalink
upgrade to latest fp-ts (0.5)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Aug 23, 2017
1 parent ece5950 commit fb23b73
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 131 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ const employee: Employee = {
}
}

function capitalize(s: string): string {
return s.substring(0, 1).toUpperCase() + s.substring(1)
}
const capitalize = (s: string): string => s.substring(0, 1).toUpperCase() + s.substring(1)

const employee2 = {
...employee,
Expand Down Expand Up @@ -73,7 +71,7 @@ company
.compose(address)
.compose(street)
.compose(name)
.modify(capitalize, employee)
.modify(capitalize)(employee)
```

Here `modify` lift a function `string => string` to a function `Employee => Employee`. It works but it would be clearer if we could zoom
Expand All @@ -86,7 +84,7 @@ import { some, none } from 'fp-ts/lib/Option'

const firstLetter = new Optional<string, string>(
s => s.length > 0 ? some(s[0]) : none,
(a, s) => a + s.substring(1)
a => s => a + s.substring(1)
)

company
Expand All @@ -95,7 +93,7 @@ company
.compose(name)
.asOptional()
.compose(firstLetter)
.modify(s => s.toUpperCase(), employee)
.modify(s => s.toUpperCase())(employee)
```

Similarly to `compose` for lenses, `compose` for optionals takes two `Optionals`, one from `A` to `B` and another from `B` to `C` and creates a third `Optional` from `A` to `C`.
Expand Down
11 changes: 8 additions & 3 deletions examples/Fold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { monoidProduct, monoidSum } from 'fp-ts/lib/Monoid'
import { identity } from 'fp-ts/lib/function'

const xs = ['a', 'bb']
const fold = fromFoldable<string>(array)
const fold = fromFoldable(array)<string>()
const len = (s: string) => s.length

console.log(fold.foldMap(monoidSum, s => s.length, xs))
console.log(fold.foldMap(monoidProduct, s => s.length, xs))
console.log(fold.foldMap(monoidSum)(len)(xs))
console.log(fold.foldMap(monoidProduct)(len)(xs))

import * as either from 'fp-ts/lib/Either'

const fold2 = fromFoldable(either)<boolean, string>()
18 changes: 9 additions & 9 deletions examples/Lens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,31 @@ export const employee: Employee = {
}
}

const company = new Lens<Employee, Company>(s => s.company, (a, s) => ({ ...s, company: a }))
const company = new Lens<Employee, Company>(s => s.company, a => s => ({ ...s, company: a }))

console.log(JSON.stringify(company.get(employee), null, 2))

const address = new Lens<Company, Address>(s => s.address, (a, s) => ({ ...s, address: a }))
const street = new Lens<Address, Street>(s => s.street, (a, s) => ({ ...s, street: a }))
const name = new Lens<Street, string>(s => s.name, (a, s) => ({ ...s, name: a }))
const address = new Lens<Company, Address>(s => s.address, a => s => ({ ...s, address: a }))
const street = new Lens<Address, Street>(s => s.street, a => s => ({ ...s, street: a }))
const name = new Lens<Street, string>(s => s.name, a => s => ({ ...s, name: a }))

// composition
const streetLens = company.compose(address).compose(street)
export const nameLens = streetLens.compose(name)

console.log(JSON.stringify(nameLens.get(employee), null, 2))

const employee2 = nameLens.modify(a => a.toUpperCase(), employee)
const employee2 = nameLens.modify(a => a.toUpperCase())(employee)

console.log(JSON.stringify(employee2, null, 2))

const employee3 = nameLens.set('low street', employee)
const employee3 = nameLens.set('low street')(employee)

console.log(JSON.stringify(employee3, null, 2))

const numLens = streetLens.compose(new Lens<Street, number>(s => s.num, (a, s) => ({ ...s, num: a })))
const numLens = streetLens.compose(new Lens<Street, number>(s => s.num, a => s => ({ ...s, num: a })))

console.log(JSON.stringify(numLens.set(42, employee), null, 2))
console.log(JSON.stringify(numLens.set(42)(employee), null, 2))

// generation
type PersonType = {
Expand All @@ -67,4 +67,4 @@ const person: PersonType = { name: 'Giulio', age: 42 }

const age = Lens.fromProp<PersonType, 'age'>('age')

console.log(age.set(43, person)) // => { name: 'Giulio', age: 43 }
console.log(age.set(43)(person)) // => { name: 'Giulio', age: 43 }
10 changes: 5 additions & 5 deletions examples/Optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { nameLens, employee } from './Lens'

const head = new Optional<string, string>(
s => (s.length > 0 ? some(s[0]) : none),
(a, s) => (s.length > 0 ? a + s.substring(1) : '')
a => s => (s.length > 0 ? a + s.substring(1) : '')
)

console.log(head.getOption('')) // => None
console.log(head.getOption('hello')) // => Some('h')
console.log(head.set('H', '')) // => ''
console.log(head.set('H', 'hello')) // => 'Hello'
console.log(head.set('H')('')) // => ''
console.log(head.set('H')('hello')) // => 'Hello'

const optional = nameLens.asOptional().compose(head)

console.log(JSON.stringify(optional.modify(s => s.toUpperCase(), employee), null, 2))
console.log(JSON.stringify(optional.modify(s => s.toUpperCase())(employee), null, 2))

interface Person {
name: string
Expand All @@ -26,4 +26,4 @@ const surname = Lens.fromProp<Person, 'surname'>('surname').composePrism(Prism.s
const p: Person = { name: 'Giulio', surname: none }

console.log(surname.getOption(p)) // => None
console.log(surname.set('Canti', p)) // => { name: 'Giulio', surname: Some("Canti") }
console.log(surname.set('Canti')(p)) // => { name: 'Giulio', surname: Some("Canti") }
8 changes: 4 additions & 4 deletions examples/Prism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ console.log(jStr.getOption(new JNum(1)))

// a function is applied only if there is a match
const reverse = (s: string): string => s.split('').reverse().join('')
console.log(jStr.modify(reverse, new JStr('hello')))
console.log(jStr.modify(reverse, new JNum(1)))
console.log(jStr.modifyOption(reverse, new JStr('hello')))
console.log(jStr.modifyOption(reverse, new JNum(1)))
console.log(jStr.modify(reverse)(new JStr('hello')))
console.log(jStr.modify(reverse)(new JNum(1)))
console.log(jStr.modifyOption(reverse)(new JStr('hello')))
console.log(jStr.modifyOption(reverse)(new JNum(1)))

// composizione
const jNum = new Prism<Json, number>(s => (s instanceof JNum ? some(s.value) : none), a => new JNum(a))
Expand Down
10 changes: 5 additions & 5 deletions examples/Traversal.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { fromTraversable } from '../src'
import { array } from 'fp-ts/lib/Array'

const eachL = fromTraversable<number>(array)
const eachL = fromTraversable(array)<number>()

const xs = [1, 2, 3, 4]

console.log(eachL.set(0, xs)) // => [ 0, 0, 0, 0 ]
console.log(eachL.set(0)(xs)) // => [ 0, 0, 0, 0 ]

console.log(eachL.modify((n: number) => n + 1, xs)) // => [ 2, 3, 4, 5 ]
console.log(eachL.modify((n: number) => n + 1)(xs)) // => [ 2, 3, 4, 5 ]

const fold = eachL.asFold()

console.log(fold.getAll(xs))
console.log(fold.headOption(xs))
console.log(fold.find(n => n > 2, xs))
console.log(fold.all((n: number) => n % 2 === 0, xs))
console.log(fold.find(n => n > 2)(xs))
console.log(fold.all((n: number) => n % 2 === 0)(xs))
4 changes: 2 additions & 2 deletions examples/introduction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const name = Lens.fromProp<Street, 'name'>('name')

import { some, none } from 'fp-ts/lib/Option'

const firstLetter = new Optional<string, string>(s => (s.length > 0 ? some(s[0]) : none), (a, s) => a + s.substring(1))
const firstLetter = new Optional<string, string>(s => (s.length > 0 ? some(s[0]) : none), a => s => a + s.substring(1))

console.log(
JSON.stringify(
Expand All @@ -68,7 +68,7 @@ console.log(
.compose(name)
.asOptional()
.compose(firstLetter)
.modify(s => s.toUpperCase(), employee),
.modify(s => s.toUpperCase())(employee),
null,
2
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"homepage": "https://github.com/gcanti/monocle-ts",
"dependencies": {
"fp-ts": "^0.4.3"
"fp-ts": "^0.5.0"
},
"devDependencies": {
"@types/mocha": "^2.2.38",
Expand Down
Loading

0 comments on commit fb23b73

Please sign in to comment.