Coder Social home page Coder Social logo

ronami / meta-typing Goto Github PK

View Code? Open in Web Editor NEW
1.5K 16.0 24.0 1.67 MB

๐Ÿ“š Functions and algorithms implemented purely with TypeScript's type system

License: MIT License

TypeScript 100.00%
typescript recursion example algorithms learning functional type-system

meta-typing's Introduction

๐Ÿ“š Meta-Typing

Functions and algorithms implemented purely with TypeScript's type system

Introduction

TypeScript's type system lets us catch bugs and errors in our code as we write it, instead of later on when the code runs. But... that's the obvious way to use the type system... ๐Ÿ˜œ

This project attempts to push TypeScript's type system to its limits by actually implementing various functions and algorithms, purely on top of the type system.

Every implementation includes comments describing in detail what's going on. Some functions and algorithms use creative (and sometimes not officially supported) solutions to overcome some limitations of the type system.

โ˜ Please note that this project is meant to be used for fun and learning purposes and not for practical use.

showcase

Try running the code

Start by installing dependencies:

$ yarn

Open a file of any function or algorithm and hover over the types to see the results of "running" that function with some input (try hovering the resulting type).

You can also run tests (written with tsd) with:

$ yarn test

Functions and algorithms:

  • Math:

    • Basic arithmetic - add one and decrease by one.
    • Add - adds two numbers.
    • Subtract - subtracts two numbers.
    • Multiply - multiplies two numbers.
    • Divide - divides two numbers.
    • Greater than or equal - checks if a value is greater than or equal to another value.
    • Less than or equal - checks if a value is less than or equal to another value.
    • Max - computes the maximum value of an array.
    • Min - computes the minimum value of an array.
    • Sum - computes the sum of the values in array.
    • Remainder - return the remainder (%) when one number is divided by a second number.
  • Lists

    • Head - gets the first element of an array.
    • Tail - gets all but the first element of an array.
    • Reverse - Reverses an array so that the first element becomes the last, the second element becomes the second to last, and so on.
    • Size - gets the size of an array.
    • Concat - creates a new array by concatenating two arrays together.
    • Drop - creates a slice of an array with n elements dropped from the beginning.
    • Take - creates a slice of an array with n elements taken from the beginning.
    • Uniq - creates a duplicate-free version of an array.
    • Includes - checks if a value is an array.
    • IndexOf - gets the index at which the first occurrence of a value is found in an array.
    • Difference - creates an array of values from the first array that are not included in the other given arrays.
    • Intersection - creates an array of unique values that are included in all given arrays.
    • Slice - creates a slice of an array from start up to, but not including, end.
    • Flatten - flattens an array a single level deep.
    • Pull - removes all given values from an array.
    • Chunk - creates an array of elements split into groups the length of size.
    • Zip - creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
    • Range - creates an array of numbers progressing from start up to, but not including, end.
  • Sorting

    • Quick-sort - an efficient sorting algorithm.
    • Merge-sort - another efficient sorting algorithm.
    • Insertion-sort - a simple sorting algorithm that builds the final sorted array (or list) one item at a time.
  • Utility

    • IsEqual - compares between two values to determine if they are equivalent.
  • Puzzles

    • N-Queens - the problem of placing N chess queens on an Nร—N chessboard so that no two queens threaten each other.
    • Maze-solving - find the shortest path to solve a maze with obstacles.
    • Binary trees - a tree data structure in which each node has at most two children.
    • Square Matrix Rotation - you are given an Nร—N 2D matrix (representing an image). Rotate the matrix by 90 degrees (clockwise and counter-clockwise).
    • Towers of Hanoi - a mathematical game or puzzle that consists of three rods and a number of disks of different sizes.

Additional links

meta-typing's People

Contributors

nickheiner avatar ranyitz avatar ronami avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meta-typing's Issues

Question for help with typing

Good afternoon. Great repository, thanks.

This question sounds more like a request for help. If you have the time and desire to help.

I'm doing a library that helps with organizing logic and business tasks.
https://github.com/avil13/vue-act-master

And I'm having a serious problem.

I create an action, and I call it through the manager.

But when I call it, I can't get the return type of the function.

Example code:

import { ActMaster, ActMasterAction } from 'act-master';

class GetUserAction implements ActMasterAction {
  name = 'get.user';

  async exec() {
    return { name: 'My Name' }
  }
}

const actions: actMasterAction[] = [new GetUserAction()];

constMaster = new ActMaster({
  actions,
});

// Here is the gist of this question
// I would like the names of the actions to be prompted
// and the type of result return
actMaster.exec('get.user');

Is it possible to create a declaration based on code?
Or create an interface that tells you the name of the action and the return result.

I tried to do something like that, but the attempt failed.

interface ActionList {
  'get.user': { name: string };
}

type exec = (name: keyof ActionList) => ActionList[keyof ActionList];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.