Trying to decrease dependencies

Foldable and other files still too much interdependent
This commit is contained in:
Elytherion
2019-02-11 01:04:19 +01:00
parent fac869e6c2
commit a8f326c190
8 changed files with 1965 additions and 1113 deletions
+1869 -1080
View File
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -54,7 +54,7 @@
"eslint": "4.19.1",
"eslint-plugin-tslint": "2.1.0",
"eslint-plugin-typescript": "0.12.0",
"jest": "24.1.0",
"jest": "23.6.0",
"jsdoc": "3.5.5",
"node-sass": "4.11.0",
"ts-jest": "23.10.5",
+3 -3
View File
@@ -10,7 +10,7 @@
import { Either, fromRight_, isEither, isRight, Right } from "../Data/Either";
import { fmap } from "../Data/Functor";
import { Cons, head, isList, isNil, List, Nil, notNull } from "../Data/List";
import { Cons, head, isList, isNil, List, Nil } from "../Data/List";
import { fromJust, isJust, isMaybe, Just, Maybe, Nothing, Some } from "../Data/Maybe";
import { showP } from "../Data/Show";
import { Identity, isIdentity, runIdentity } from "./Monad/Identity";
@@ -145,7 +145,7 @@ export const alt: AlternativeAlt =
(x: any) => (y: any): any => {
if (isList (x)) {
if (isList (y)) {
return notNull (x) ? x : y
return isNil (x) ? y : x
}
throw new TypeError (alternativeDifferentInstanceErrorMsg ("<*>") (x) (y))
@@ -177,7 +177,7 @@ interface AlternativeAlt_ {
export const alt_: AlternativeAlt_ =
(x: any) => (g: () => any): any => {
if (isList (x)) {
return notNull (x) ? x : g ()
return isNil (x) ? g () : x
}
if (isMaybe (x)) {
+3 -4
View File
@@ -12,7 +12,6 @@
import { pipe } from "ramda";
import { Either, isEither, isRight, Right } from "../Data/Either";
import { fnull } from "../Data/Foldable";
import { ident } from "../Data/Function";
import { fmap } from "../Data/Functor";
import { append, cons, head, isList, isNil, List, tail } from "../Data/List";
@@ -183,7 +182,7 @@ export const sequence: MonadSequence =
(t: MonadStr) =>
(xs: List<any>): any => {
if (t === "List") {
if (fnull (xs)) {
if (isNil (xs)) {
return pure ("List") (empty ("List"))
}
@@ -196,7 +195,7 @@ export const sequence: MonadSequence =
}
if (t === "Either") {
if (fnull (xs)) {
if (isNil (xs)) {
return Right (empty ("List"))
}
@@ -209,7 +208,7 @@ export const sequence: MonadSequence =
}
if (t === "Maybe") {
if (fnull (xs)) {
if (isNil (xs)) {
return Just (empty ("List"))
}
+6 -6
View File
@@ -5,11 +5,11 @@
*/
import { isEither, isRight } from "./Either";
import { all, fnull } from "./Foldable";
import { isList, notNull } from "./List";
import { all, elem, fnull } from "./Foldable";
import { isList } from "./List";
import { isJust, isMaybe, isNothing, Maybe, Some } from "./Maybe";
import { isOrderedMap, OrderedMap } from "./OrderedMap";
import { isOrderedSet, member, OrderedSet } from "./OrderedSet";
import { isOrderedSet, OrderedSet } from "./OrderedSet";
import { isPair } from "./Pair";
import { isRecord, Record } from "./Record";
import { show } from "./Show";
@@ -45,8 +45,8 @@ export const equals =
(xs1: any, xs2: any): boolean =>
fnull (xs1)
&& fnull (xs2)
|| notNull (xs1)
&& notNull (xs2)
|| !fnull (xs1)
&& !fnull (xs2)
&& equals (xs1 .x) (xs2 .x)
&& equalsCons (xs1 .xs, xs2 .xs)
@@ -153,7 +153,7 @@ export const notEquals =
!equals (m1) (m2)
const getRecordField = <A> (key: keyof A) => (r: Record<A>) => {
if (member (key as string) (r .keys)) {
if (elem (key as string) (r .keys)) {
const specifiedValue = r .values [key]
// tslint:disable-next-line: strict-type-predicates
+74 -16
View File
@@ -1,13 +1,11 @@
import { pipe } from "ramda";
import { add, inc, max, min, multiply } from "../App/Utils/mathUtils";
import { not } from "../App/Utils/not";
import { empty, pure } from "../Control/Applicative";
import { Either, fromRight, isEither, isLeft, isRight, Left, Right } from "./Either";
import { Either, isEither, isLeft, isRight, Left, Right } from "./Either";
import { equals } from "./Eq";
import { ident } from "./Function";
import { fmap } from "./Functor";
import { append, Cons, isList, isNil, List, Nil, NonEmptyList } from "./List";
import { fromMaybe, isJust, isMaybe, isNothing, Just, Maybe, Nothing } from "./Maybe";
import { isJust, isMaybe, isNothing, Just, Maybe, Nothing } from "./Maybe";
import { isOrderedMap, OrderedMap } from "./OrderedMap";
import { isOrderedSet, OrderedSet } from "./OrderedSet";
import { fromBinary, Pair } from "./Pair";
@@ -266,11 +264,11 @@ export const toList: FoldableToList =
}
if (isEither (x)) {
return isRight (x) ? pure ("List") (x .value) : empty ("List")
return isRight (x) ? Cons (x .value, Nil) : Nil
}
if (isMaybe (x)) {
return isJust (x) ? pure ("List") (x .value) : empty ("List")
return isJust (x) ? Cons (x .value, Nil) : Nil
}
throw new TypeError (instanceErrorMsg ("toList") (x))
@@ -424,7 +422,7 @@ export const minimum = foldr (min) (Infinity)
*/
export const concat =
<A> (x: Foldable<List<A>>): List<A> =>
foldr<List<A>, List<A>> (append) (empty ("List")) (x)
foldr<List<A>, List<A>> (append) (Nil) (x)
/**
* `concatMap :: Foldable t => (a -> [b]) -> t a -> [b]`
@@ -434,7 +432,7 @@ export const concat =
*/
export const concatMap =
<A, B> (f: (x: A) => List<B>) =>
foldr<A, List<B>> (pipe (f, append)) (empty ("List"))
foldr<A, List<B>> (pipe (f, append)) (Nil)
/**
* `and :: Foldable t => t Bool -> Bool`
@@ -463,11 +461,19 @@ export const and =
}
if (isEither (x)) {
return fromRight (true) (x)
if (isLeft (x)) {
return true
}
return x .value
}
if (isMaybe (x)) {
return fromMaybe (true) (x)
if (isNothing (x)) {
return true
}
return x .value
}
throw new TypeError (instanceErrorMsg ("and") (x))
@@ -500,11 +506,19 @@ export const or =
}
if (isEither (x)) {
return fromRight (false) (x)
if (isLeft (x)) {
return false
}
return x .value
}
if (isMaybe (x)) {
return fromMaybe (false) (x)
if (isNothing (x)) {
return false
}
return x .value
}
throw new TypeError (instanceErrorMsg ("or") (x))
@@ -548,11 +562,19 @@ export const any: FoldableAny =
}
if (isEither (x)) {
return fromRight (false) (fmap (f) (x))
if (isLeft (x)) {
return false
}
return f (x .value)
}
if (isMaybe (x)) {
return fromMaybe (false) (fmap (f) (x))
if (isNothing (x)) {
return false
}
return f (x .value)
}
throw new TypeError (instanceErrorMsg ("any") (x))
@@ -578,11 +600,19 @@ export const all =
}
if (isEither (x)) {
return fromRight (true) (fmap (f) (x))
if (isLeft (x)) {
return true
}
return f (x .value)
}
if (isMaybe (x)) {
return fromMaybe (true) (fmap (f) (x))
if (isNothing (x)) {
return true
}
return f (x .value)
}
throw new TypeError (instanceErrorMsg ("all") (x))
@@ -667,3 +697,31 @@ const emptyErrorMsg =
(fname: string) =>
(x: any) =>
`${fname}: empty structure\n${showP (x)}`
export const Foldable = {
foldr,
foldl,
foldr1,
foldl1,
toList,
fnull,
length,
elem,
elemF,
sum,
product,
maximum,
minimum,
concat,
concatMap,
and,
or,
any,
all,
notElem,
notElemF,
find,
}
+4 -3
View File
@@ -7,8 +7,9 @@
* @author Lukas Obermann
*/
import { not, pipe } from "ramda";
import { foldl } from "./Foldable";
import { pipe } from "ramda";
import { not } from "../App/Utils/not";
import { elem, foldl } from "./Foldable";
import { Lens, lens } from "./Lens";
import { isJust, isMaybe, isNothing, Maybe, Nothing } from "./Maybe";
import { fromArray, OrderedSet } from "./OrderedSet";
@@ -269,7 +270,7 @@ const makeAccessors =
* Is the key a member of the record?
*/
export const member =
(key: string) => (mp: Record<any>): boolean => OrderedSet.member (key) (mp .keys)
(key: string) => (mp: Record<any>): boolean => elem (key) (mp .keys)
/**
* `notMember :: String -> Record a -> Bool`
+5
View File
@@ -1,5 +1,6 @@
// @ts-check
const React = require('react');
const { Foldable } = require('../Foldable');
const { List } = require('../List');
const { Maybe } = require('../Maybe');
const { fmap } = require('../Functor');
@@ -156,6 +157,10 @@ test ('listToMaybe', () => {
})
test ('maybeToList', () => {
expect (typeof Foldable)
.toBe ("object")
expect (typeof Maybe.maybeToList)
.toBe ("function")
expect (Maybe.maybeToList (Just (3)))
.toEqual (List (3))
expect (Maybe.maybeToList (Nothing))