Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5ea023
commit 778b4a2
Showing
5 changed files
with
146 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('lens', () => { | ||
|
||
eq (S.show (S.lens)) ('lens :: (s -> a) -> (a -> s -> s) -> Any'); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('over', () => { | ||
|
||
eq (S.show (S.over)) ('over :: Any -> Any -> Any -> Any'); | ||
|
||
const snd = S.lens (S.snd) (x => S.map (S.K (x))); | ||
const fahrenheit = S.lens (c => c * 9 / 5 + 32) (f => c => (f - 32) * 5 / 9); | ||
|
||
// Increase the temperature in San Francisco by 1 °C | ||
eq (S.over (snd) (S.add (1)) (S.Pair ('San Francisco') (20))) | ||
(S.Pair ('San Francisco') (21)); | ||
|
||
// 1 °F above freezing, in °C | ||
eq (S.over (fahrenheit) (S.add (1)) (0)) (0.5555555555555556); | ||
|
||
// 1 °F above freezing, in °F | ||
eq (S.view (fahrenheit) (S.over (fahrenheit) (S.add (1)) (0))) (33); | ||
|
||
// Decrease the temperature in San Francisco by 4 °F | ||
eq (S.over (S.compose (snd) (fahrenheit)) | ||
(S.sub (4)) | ||
(S.Pair ('San Francisco') (20))) | ||
(S.Pair ('San Francisco') (17.77777777777778)); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('set', () => { | ||
|
||
eq (S.show (S.set)) ('set :: TK'); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
const S = require ('..'); | ||
|
||
const eq = require ('./internal/eq'); | ||
|
||
|
||
test ('view', () => { | ||
|
||
eq (S.show (S.view)) ('view :: Any -> Any -> Any'); | ||
|
||
const snd = S.lens (S.snd) (x => S.map (S.K (x))); | ||
const fahrenheit = S.lens (c => c * 9 / 5 + 32) (f => c => (f - 32) * 5 / 9); | ||
|
||
eq (S.view (snd) (S.Pair ('San Francisco') (20))) (20); | ||
|
||
eq (S.view (fahrenheit) (0)) (32); | ||
eq (S.view (fahrenheit) (50)) (122); | ||
eq (S.view (fahrenheit) (100)) (212); | ||
|
||
// Temperature in San Francisco, converted to °F | ||
eq (S.view (S.compose (snd) (fahrenheit)) | ||
(S.Pair ('San Francisco') (20))) | ||
(68); | ||
|
||
// Lenses compose via function composition | ||
eq (S.view (x => snd (fahrenheit (x))) | ||
(S.Pair ('San Francisco') (20))) | ||
(68); | ||
|
||
}); |