From c7df99b2124d05d78b70eff02be42c0ccb58c614 Mon Sep 17 00:00:00 2001 From: Guy Kedem Date: Wed, 18 Dec 2024 13:35:41 +0700 Subject: [PATCH] no commit message --- deno.json | 4 - functions/curry.ts | 77 ---------- functions/deno.json | 1 - functions/mod.ts | 1 - functions/pipe.ts | 16 +- functions/pipe_test.ts | 32 ++++ functions/set_arguments.ts | 295 ------------------------------------- 7 files changed, 42 insertions(+), 384 deletions(-) delete mode 100644 functions/curry.ts create mode 100644 functions/pipe_test.ts delete mode 100644 functions/set_arguments.ts diff --git a/deno.json b/deno.json index 1ecfbe7db091c..a24ee15a614c4 100644 --- a/deno.json +++ b/deno.json @@ -6,7 +6,6 @@ "noImplicitOverride": true, "noUncheckedIndexedAccess": true }, -<<<<<<< HEAD "imports": { "@deno/doc": "jsr:@deno/doc@0.137", "@deno/graph": "jsr:@deno/graph@^0.74", @@ -53,9 +52,6 @@ "graphviz": "npm:node-graphviz@^0.1.1", "npm:/typescript": "npm:typescript@5.4.4" }, -======= - "importMap": "./import_map.json", ->>>>>>> 05b6d7eedd8e441cb8fa22078f377a6a37b4fa88 "tasks": { "test": "deno test --unstable-http --unstable-webgpu --doc --allow-all --parallel --coverage --trace-leaks --clean", "test:browser": "git grep --name-only \"This module is browser compatible.\" | grep -v deno.json | grep -v .github/workflows | grep -v _tools | grep -v encoding/README.md | grep -v media_types/vendor/update.ts | xargs deno check --config browser-compat.tsconfig.json", diff --git a/functions/curry.ts b/functions/curry.ts deleted file mode 100644 index dc0207fb6eade..0000000000000 --- a/functions/curry.ts +++ /dev/null @@ -1,77 +0,0 @@ -// deno-lint-ignore no-explicit-any -type AnyFunction = (...args: any[]) => any; - -type Curried1Function = { - (p1: P1): T; -}; -type Curried2Function = { - (p1: P1): Curried1Function; - (p1: P1, p2: P2): T; -}; -type Curried3Function = { - (p1: P1): Curried2Function; - (p1: P1, p2: P2): Curried1Function; - (p1: P1, p2: P2, p3: P3): T; -}; -type Curried4Function = { - (p1: P1): Curried3Function; - (p1: P1, p2: P2): Curried2Function; - (p1: P1, p2: P2, p3: P3): Curried1Function; - (p1: P1, p2: P2, p3: P3, p4: P4): T; -}; - -/** - * A function that returns a curried version of a given function - * - * @param {(p1: P1, p2: P2, … pn: Pn) => T} fn - The function to be curried. - * @returns - A function which can be provided with only some of the arguments of the given function at each invocation, once all arguments have been provided the given function is called. - * @example Usage - * function add(a: number, b: number, c: number) { - * return a + b + c + d; - * } - * - * const curriedAdd = curry(add); - * console.log(curriedAdd(1)(2)(3)); // 6 - * console.log(curriedAdd(1, 2)(3)); // 6 - * console.log(curriedAdd(1, 2, 3)); // 6 - */ - -export function curry( - fn: () => T, -): () => T; -export function curry( - fn: (p1: P1) => T, -): (p1: P1) => T; -export function curry( - fn: (p1: P1, p2: P2) => T, -): Curried2Function; -export function curry( - fn: (p1: P1, p2: P2, p3: P3) => T, -): Curried3Function; -export function curry( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, -): Curried4Function; -export function curry(fn: AnyFunction) { - return function curried(...args: any[]): any { - if (args.length >= fn.length) { - return fn(...args); - } else { - return (...moreArgs: any[]) => curried(...args, ...moreArgs); - } - }; -} - -function add(a: number, b: number, c: number, d: number) { - return a + b + c + d; -} - -const curriedAdd = curry(add); - -if (import.meta.main) { - const fnn = curriedAdd(1, 2); - console.log(curriedAdd(1)(2)(3)(4)); - console.log(curriedAdd(1, 2)(3)(4)); - console.log(curriedAdd(1, 2, 3)(4)); - console.log(curriedAdd(1, 2, 3, 4)); - console.log(curriedAdd(1, 2, 3, 4)); -} diff --git a/functions/deno.json b/functions/deno.json index 17e15210ebc89..7cb527aba535c 100644 --- a/functions/deno.json +++ b/functions/deno.json @@ -3,7 +3,6 @@ "version": "0.1.0", "exports": { ".": "./mod.ts", - "./curry": "./curry.ts", "./pipe": "./pipe.ts" } } diff --git a/functions/mod.ts b/functions/mod.ts index b121a95548c1d..7aa800e41fca1 100644 --- a/functions/mod.ts +++ b/functions/mod.ts @@ -28,5 +28,4 @@ * @module */ -export * from "./curry.ts"; export * from "./pipe.ts"; diff --git a/functions/pipe.ts b/functions/pipe.ts index e5c755b5cf974..542068d29a726 100644 --- a/functions/pipe.ts +++ b/functions/pipe.ts @@ -17,15 +17,19 @@ type PipeArgs = F extends [ : Acc : Acc; +export function pipe(): (arg: T) => T; export function pipe( firstFn: FirstFn, ...fns: PipeArgs extends F ? F : PipeArgs -): (arg: Parameters[0]) => LastFnReturnType> { +): (arg: Parameters[0]) => LastFnReturnType>; + +export function pipe( + firstFn?: FirstFn, + ...fns: PipeArgs extends F ? F : PipeArgs +): any { + if (!firstFn) { + return (arg: T) => arg; + } return (arg: Parameters[0]) => (fns as AnyFunc[]).reduce((acc, fn) => fn(acc), firstFn(arg)); } - -if (import.meta.main) { - const res = pipe(Math.abs, Math.sqrt, Math.floor); - res(-2); // 1 -} diff --git a/functions/pipe_test.ts b/functions/pipe_test.ts new file mode 100644 index 0000000000000..7632928fc8abd --- /dev/null +++ b/functions/pipe_test.ts @@ -0,0 +1,32 @@ +// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. + +import { assertEquals, assertThrows } from "@std/assert"; +import { pipe } from "./mod.ts"; + +Deno.test("pipe() handles mixed types", () => { + const inputPipe = pipe( + Math.abs, + Math.sqrt, + Math.floor, + (num) => `result: ${num}`, + ); + assertEquals(inputPipe(-2), "result: 1"); +}); + +Deno.test("en empty pipe is the identity function", () => { + const inputPipe = pipe(); + assertEquals(inputPipe("hello"), "hello"); +}); + +Deno.test("pipe() throws an exceptions when a function throws an exception", () => { + const inputPipe = pipe( + Math.abs, + Math.sqrt, + Math.floor, + (num) => { + throw new Error("This is an error for " + num); + }, + (num) => `result: ${num}`, + ); + assertThrows(() => inputPipe(-2)); +}); diff --git a/functions/set_arguments.ts b/functions/set_arguments.ts deleted file mode 100644 index 6302e20a79de3..0000000000000 --- a/functions/set_arguments.ts +++ /dev/null @@ -1,295 +0,0 @@ -export function set_arguments(fn: (p1: P1) => T, p1: P1): () => T; -export function set_arguments( - fn: (p1: P1, p2: P2) => T, - p1: P1, - p2: undefined, -): (p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2) => T, - p1: undefined, - p2: P2, -): (p1: P1) => T; -export function set_arguments( - fn: (p1: P1, p2: P2) => T, - p1: P1, - p2: P2, -): () => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: P1, - p2: P2, - p3: undefined, -): (p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: P1, - p2: undefined, - p3: P3, -): (p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: undefined, - p2: P2, - p3: P3, -): (p1: P1) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: P1, - p2: undefined, - p3: undefined, -): (p2: P2, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: undefined, - p2: P2, - p3: undefined, -): (p1: P1, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: undefined, - p2: undefined, - p3: P3, -): (p1: P1, p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3) => T, - p1: P1, - p2: P2, - p3: P3, -): () => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: P2, - p3: P3, - p4: undefined, -): (p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: P2, - p3: undefined, - p4: P4, -): (p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: undefined, - p3: P3, - p4: P4, -): (p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: undefined, - p2: P2, - p3: P3, - p4: P4, -): (p1: P1) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: P2, - p3: undefined, - p4: undefined, -): (p3: P3, p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: undefined, - p3: P3, - p4: undefined, -): (p2: P2, p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: undefined, - p2: P2, - p3: undefined, - p4: P4, -): (p1: P1, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: undefined, - p2: undefined, - p3: P3, - p4: P4, -): (p1: P1, p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: undefined, - p2: P2, - p3: P3, - p4: undefined, -): (p1: P1, p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: undefined, - p3: undefined, - p4: P4, -): (p2: P2, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: undefined, - p2: undefined, - p3: undefined, - p4: P4, -): (p1: P1, p2: P2, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4) => T, - p1: P1, - p2: P2, - p3: P3, - p4: P4, -): () => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: P3, - p4: P4, - p5: undefined, -): (p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: P3, - p4: undefined, - p5: P5, -): (p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: undefined, - p4: P4, - p5: P5, -): (p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: undefined, - p3: P3, - p4: P4, - p5: P5, -): (p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: P2, - p3: P3, - p4: P4, - p5: P5, -): (p1: P1) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: P3, - p4: undefined, - p5: undefined, -): (p4: P4, p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: undefined, - p4: P4, - p5: undefined, -): (p3: P3, p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: undefined, - p3: P3, - p4: undefined, - p5: P5, -): (p2: P2, p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: P2, - p3: P3, - p4: undefined, - p5: P5, -): (p1: P1, p4: P4) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: undefined, - p3: undefined, - p4: P4, - p5: P5, -): (p2: P2, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: P2, - p3: undefined, - p4: P4, - p5: P5, -): (p1: P1, p3: P3) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: undefined, - p3: P3, - p4: P4, - p5: P5, -): (p1: P1, p2: P2) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: P2, - p3: P3, - p4: P4, - p5: undefined, -): (p1: P1, p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: undefined, - p3: undefined, - p4: P4, - p5: undefined, -): (p2: P2, p3: P3, p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: undefined, - p2: undefined, - p3: P3, - p4: P4, - p5: undefined, -): (p1: P1, p2: P2, p5: P5) => T; -export function set_arguments( - fn: (p1: P1, p2: P2, p3: P3, p4: P4, p5: P5) => T, - p1: P1, - p2: P2, - p3: P3, - p4: P4, - p5: P5, -): () => T; -export function set_arguments( - fn: (...args: any[]) => any, - ...setArgs: any[] -) { - // ensure setArgs at least as long as the function's arguments - while (setArgs.length < fn.length) { - setArgs.push(undefined); - } - - return (...providedArgs: any[]) => { - // insert each argument at the index of undefined - const mergedArgs = setArgs.map((arg) => - arg === undefined ? providedArgs.shift() : arg - ); - - return fn(...mergedArgs); - }; -} - -if (import.meta.main) { - const divide = (a: number, b: number) => a / b; - const invert = set_arguments(divide, 1, undefined); - const r = invert(2); - console.log(r); // 1/2 -}