recursive-type

How to reduce code duplication when dealing with recursive sum types

好久不见. 提交于 2020-02-17 05:16:34
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

How to reduce code duplication when dealing with recursive sum types

百般思念 提交于 2020-02-17 05:15:07
问题 I am currently working on a simple interpreter for a programming language and I have a data type like this: data Expr = Variable String | Number Int | Add [Expr] | Sub Expr Expr And I have many functions that do simple things like: -- Substitute a value for a variable substituteName :: String -> Int -> Expr -> Expr substituteName name newValue = go where go (Variable x) | x == name = Number newValue go (Add xs) = Add $ map go xs go (Sub x y) = Sub (go x) (go y) go other = other -- Replace

Is it possible to create a recursive function type in Kotlin?

与世无争的帅哥 提交于 2019-12-08 20:42:52
问题 I have functions that represent steps in a process. Each function also knows the next step, if there is one. I'd like to be able to do something like: fun fooStep() : Step? { ... do something ... return ::barStep // the next step is barStep } These functions are called from a central dispatching function, which contains code a bit like this: var step = startStep while (step != null) { step = step() } Note that the logic in a particular step also determines the next step, if there even is one.

Typescript: how to achieve a recursion in this type?

霸气de小男生 提交于 2019-12-02 02:23:46
问题 My original problem is this. I made the following type but it does not work due to the circular references error and I don't know how to solve it: type Increment<T extends number, Tuple extends any[] = [any]> = T extends 0 ? 1 : T extends 1 ? 2 : T extends TupleUnshift<any, Tuple> ? TupleUnshift<any, TupleUnshift<any, Tuple>>['length'] : Increment<T, TupleUnshift<any, TupleUnshift<any, Tuple>>> In the end it should work like: type five = 5 type six = Increment<five> // 6 P.S. TupleUnshift is

Typescript: how to achieve a recursion in this type?

佐手、 提交于 2019-12-01 21:18:06
My original problem is this . I made the following type but it does not work due to the circular references error and I don't know how to solve it: type Increment<T extends number, Tuple extends any[] = [any]> = T extends 0 ? 1 : T extends 1 ? 2 : T extends TupleUnshift<any, Tuple> ? TupleUnshift<any, TupleUnshift<any, Tuple>>['length'] : Increment<T, TupleUnshift<any, TupleUnshift<any, Tuple>>> In the end it should work like: type five = 5 type six = Increment<five> // 6 P.S. TupleUnshift is from here . I think in the other question and in comments I said that you can try to do this

“overflow while adding drop-check rules” while implementing a fingertree

烂漫一生 提交于 2019-11-28 13:51:03
I'm trying to define a finger tree structure and implement its basic operations as an exercise in Rust. I've come up with the following, which is basically what's described in this paper . use self::FingerTree::{Empty, Single, Deep}; use self::Digit::{One, Two, Three, Four}; enum Digit<A> { One(A), Two(A, A), Three(A, A, A), Four(A, A, A, A), } enum Node<V, A> { Node2(V, A, A), Node3(V, A, A, A), } enum FingerTree<V, A> { Empty, Single(A), Deep { size: V, prefix: Digit<A>, tree: Box<FingerTree<V, Node<V, A>>>, suffix: Digit<A>, }, } fn main() { let e: FingerTree<i32, String> = Empty; }

“overflow while adding drop-check rules” while implementing a fingertree

…衆ロ難τιáo~ 提交于 2019-11-27 08:01:40
问题 I'm trying to define a finger tree structure and implement its basic operations as an exercise in Rust. I've come up with the following, which is basically what's described in this paper. use self::FingerTree::{Empty, Single, Deep}; use self::Digit::{One, Two, Three, Four}; enum Digit<A> { One(A), Two(A, A), Three(A, A, A), Four(A, A, A, A), } enum Node<V, A> { Node2(V, A, A), Node3(V, A, A, A), } enum FingerTree<V, A> { Empty, Single(A), Deep { size: V, prefix: Digit<A>, tree: Box<FingerTree