Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
5
Task/Parametric-polymorphism/00-META.yaml
Normal file
5
Task/Parametric-polymorphism/00-META.yaml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
category:
|
||||
- Type System
|
||||
from: http://rosettacode.org/wiki/Parametric_polymorphism
|
||||
note: Basic language learning
|
||||
12
Task/Parametric-polymorphism/00-TASK.txt
Normal file
12
Task/Parametric-polymorphism/00-TASK.txt
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
[[wp:Parametric Polymorphism|Parametric Polymorphism]] is a way to define types or functions that are generic over other types. The genericity can be expressed by using ''type variables'' for the parameter type, and by a mechanism to explicitly or implicitly replace the type variables with concrete types when necessary.
|
||||
|
||||
|
||||
;Task:
|
||||
Write a small example for a type declaration that is parametric over another type, together with a short bit of code (and its type signature) that uses it.
|
||||
|
||||
|
||||
A good example is a container type, let's say a binary tree, together with some function that traverses the tree, say, a ''map''-function that operates on every element of the tree.
|
||||
|
||||
This language feature only applies to statically-typed languages.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
generic
|
||||
type Element_Type is private;
|
||||
package Container is
|
||||
type Tree is tagged private;
|
||||
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type);
|
||||
private
|
||||
type Node;
|
||||
type Node_Access is access Node;
|
||||
type Tree tagged record
|
||||
Value : Element_type;
|
||||
Left : Node_Access := null;
|
||||
Right : Node_Access := null;
|
||||
end record;
|
||||
end Container;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package body Container is
|
||||
procedure Replace_All(The_Tree : in out Tree; New_Value : Element_Type) is
|
||||
begin
|
||||
The_Tree.Value := New_Value;
|
||||
If The_Tree.Left /= null then
|
||||
The_Tree.Left.all.Replace_All(New_Value);
|
||||
end if;
|
||||
if The_tree.Right /= null then
|
||||
The_Tree.Right.all.Replace_All(New_Value);
|
||||
end if;
|
||||
end Replace_All;
|
||||
end Container;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
template<class T>
|
||||
class tree
|
||||
{
|
||||
T value;
|
||||
tree *left;
|
||||
tree *right;
|
||||
public:
|
||||
void replace_all (T new_value);
|
||||
};
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
template<class T>
|
||||
void tree<T>::replace_all (T new_value)
|
||||
{
|
||||
value = new_value;
|
||||
if (left != NULL)
|
||||
left->replace_all (new_value);
|
||||
if (right != NULL)
|
||||
right->replace_all (new_value);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
|
||||
class BinaryTree<T>
|
||||
{
|
||||
public T value;
|
||||
public BinaryTree<T> left;
|
||||
public BinaryTree<T> right;
|
||||
|
||||
public BinaryTree(T value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public BinaryTree<U> Map<U>(Func<T, U> f)
|
||||
{
|
||||
BinaryTree<U> tree = new BinaryTree<U>(f(this.value));
|
||||
if (this.left != null)
|
||||
{
|
||||
tree.left = this.left.Map(f);
|
||||
}
|
||||
if (this.right != null)
|
||||
{
|
||||
tree.right = this.right.Map(f);
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
BinaryTree<int> b = new BinaryTree<int>(6);
|
||||
b.left = new BinaryTree<int>(5);
|
||||
b.right = new BinaryTree<int>(7);
|
||||
|
||||
BinaryTree<double> b2 = b.Map(x => x * 0.5);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,48 @@
|
|||
using System;
|
||||
|
||||
class BinaryTree<T>
|
||||
{
|
||||
public BinaryTree<T> Left { get; }
|
||||
public BinaryTree<T> Right { get; }
|
||||
public T Value { get; }
|
||||
|
||||
public BinaryTree(T value, BinaryTree<T> left = null, BinaryTree<T> right = null)
|
||||
{
|
||||
this.Value = value;
|
||||
this.Left = left;
|
||||
this.Right = right;
|
||||
}
|
||||
|
||||
public BinaryTree<U> Map<U>(Func<T, U> f)
|
||||
{
|
||||
return new BinaryTree<U>(f(this.Value), this.Left?.Map(f), this.Right?.Map(f));
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
var sb = new System.Text.StringBuilder();
|
||||
this.ToString(sb, 0);
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
private void ToString(System.Text.StringBuilder sb, int depth)
|
||||
{
|
||||
sb.Append(new string('\t', depth));
|
||||
sb.AppendLine(this.Value?.ToString());
|
||||
this.Left?.ToString(sb, depth + 1);
|
||||
this.Right?.ToString(sb, depth + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
var b = new BinaryTree<int>(6, new BinaryTree<int>(5), new BinaryTree<int>(7));
|
||||
|
||||
BinaryTree<double> b2 = b.Map(x => x * 0.5);
|
||||
|
||||
Console.WriteLine(b);
|
||||
Console.WriteLine(b2);
|
||||
}
|
||||
}
|
||||
47
Task/Parametric-polymorphism/C/parametric-polymorphism.c
Normal file
47
Task/Parametric-polymorphism/C/parametric-polymorphism.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define decl_tree_type(T) \
|
||||
typedef struct node_##T##_t node_##T##_t, *node_##T; \
|
||||
struct node_##T##_t { node_##T left, right; T value; }; \
|
||||
\
|
||||
node_##T node_##T##_new(T v) { \
|
||||
node_##T node = malloc(sizeof(node_##T##_t)); \
|
||||
node->value = v; \
|
||||
node->left = node->right = 0; \
|
||||
return node; \
|
||||
} \
|
||||
node_##T node_##T##_insert(node_##T root, T v) { \
|
||||
node_##T n = node_##T##_new(v); \
|
||||
while (root) { \
|
||||
if (root->value < n->value) \
|
||||
if (!root->left) return root->left = n; \
|
||||
else root = root->left; \
|
||||
else \
|
||||
if (!root->right) return root->right = n; \
|
||||
else root = root->right; \
|
||||
} \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
#define tree_node(T) node_##T
|
||||
#define node_insert(T, r, x) node_##T##_insert(r, x)
|
||||
#define node_new(T, x) node_##T##_new(x)
|
||||
|
||||
decl_tree_type(double);
|
||||
decl_tree_type(int);
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
tree_node(double) root_d = node_new(double, (double)rand() / RAND_MAX);
|
||||
|
||||
for (i = 0; i < 10000; i++)
|
||||
node_insert(double, root_d, (double)rand() / RAND_MAX);
|
||||
|
||||
tree_node(int) root_i = node_new(int, rand());
|
||||
for (i = 0; i < 10000; i++)
|
||||
node_insert(int, root_i, rand());
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
Task/Parametric-polymorphism/C3/parametric-polymorphism-1.c3
Normal file
15
Task/Parametric-polymorphism/C3/parametric-polymorphism-1.c3
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
module tree<Type>;
|
||||
|
||||
struct Tree
|
||||
{
|
||||
Type value;
|
||||
Tree* left;
|
||||
Tree* right;
|
||||
}
|
||||
|
||||
fn void Tree.replaceAll(Tree* a_tree, Type new_value)
|
||||
{
|
||||
a_tree.value = new_value;
|
||||
if (a_tree.left) a_tree.left.replaceAll(new_value);
|
||||
if (a_tree.right) a_tree.right.replaceAll(new_value);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
define IntTree = tree<int>::Tree;
|
||||
|
||||
fn void test()
|
||||
{
|
||||
IntTree inttree;
|
||||
inttree.replaceAll(3);
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
class BinaryTree<Data>(shared Data data, shared BinaryTree<Data>? left = null, shared BinaryTree<Data>? right = null) {
|
||||
|
||||
shared BinaryTree<NewData> myMap<NewData>(NewData f(Data d)) =>
|
||||
BinaryTree {
|
||||
data = f(data);
|
||||
left = left?.myMap(f);
|
||||
right = right?.myMap(f);
|
||||
};
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
value tree1 = BinaryTree {
|
||||
data = 3;
|
||||
left = BinaryTree {
|
||||
data = 4;
|
||||
};
|
||||
right = BinaryTree {
|
||||
data = 5;
|
||||
left = BinaryTree {
|
||||
data = 6;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
tree1.myMap(print);
|
||||
print("");
|
||||
|
||||
value tree2 = tree1.myMap((x) => x * 333.33);
|
||||
tree2.myMap(print);
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
::Tree a = Empty | Node a (Tree a) (Tree a)
|
||||
|
||||
mapTree :: (a -> b) (Tree a) -> (Tree b)
|
||||
mapTree f Empty = Empty
|
||||
mapTree f (Node x l r) = Node (f x) (mapTree f l) (mapTree f r)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
instance Functor Tree where
|
||||
fmap f Empty = Empty
|
||||
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
add1Everywhere :: (f a) -> (f a) | Functor f & Num a
|
||||
add1Everywhere nums = fmap (\x = x + 1) nums
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(deftype pair (&key (car 't) (cdr 't))
|
||||
`(cons ,car ,cdr))
|
||||
46
Task/Parametric-polymorphism/D/parametric-polymorphism.d
Normal file
46
Task/Parametric-polymorphism/D/parametric-polymorphism.d
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
class ArrayTree(T, uint N) {
|
||||
T[N] data;
|
||||
typeof(this) left, right;
|
||||
|
||||
this(T initValue) { this.data[] = initValue; }
|
||||
|
||||
void tmap(const void delegate(ref typeof(data)) dg) {
|
||||
dg(this.data);
|
||||
if (left) left.tmap(dg);
|
||||
if (right) right.tmap(dg);
|
||||
}
|
||||
}
|
||||
|
||||
void main() { // Demo code.
|
||||
import std.stdio;
|
||||
|
||||
// Instantiate the template ArrayTree of three doubles.
|
||||
alias AT3 = ArrayTree!(double, 3);
|
||||
|
||||
// Allocate the tree root.
|
||||
auto root = new AT3(1.00);
|
||||
|
||||
// Add some nodes.
|
||||
root.left = new AT3(1.10);
|
||||
root.left.left = new AT3(1.11);
|
||||
root.left.right = new AT3(1.12);
|
||||
|
||||
root.right = new AT3(1.20);
|
||||
root.right.left = new AT3(1.21);
|
||||
root.right.right = new AT3(1.22);
|
||||
|
||||
// Now the tree has seven nodes.
|
||||
|
||||
// Show the arrays of the whole tree.
|
||||
//root.tmap(x => writefln("%(%.2f %)", x));
|
||||
root.tmap((ref x) => writefln("%(%.2f %)", x));
|
||||
|
||||
// Modify the arrays of the whole tree.
|
||||
//root.tmap((x){ x[] += 10; });
|
||||
root.tmap((ref x){ x[] += 10; });
|
||||
|
||||
// Show the arrays of the whole tree again.
|
||||
writeln();
|
||||
//root.tmap(x => writefln("%(%.2f %)", x));
|
||||
root.tmap((ref x) => writefln("%(%.2f %)", x));
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
class TreeNode<T> {
|
||||
|
||||
T value;
|
||||
TreeNode<T> left;
|
||||
TreeNode<T> right;
|
||||
|
||||
TreeNode(this.value);
|
||||
|
||||
TreeNode map(T f(T t)) {
|
||||
var node = new TreeNode(f(value));
|
||||
if(left != null) {
|
||||
node.left = left.map(f);
|
||||
}
|
||||
if(right != null) {
|
||||
node.right = right.map(f);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
void forEach(void f(T t)) {
|
||||
f(value);
|
||||
if(left != null) {
|
||||
left.forEach(f);
|
||||
}
|
||||
if(right != null) {
|
||||
right.forEach(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
TreeNode root = new TreeNode(1);
|
||||
root.left = new TreeNode(2);
|
||||
root.right = new TreeNode(3);
|
||||
root.left.right = new TreeNode(4);
|
||||
|
||||
print('first tree');
|
||||
root.forEach(print);
|
||||
var newRoot = root.map((t) => t * 222);
|
||||
print('second tree');
|
||||
newRoot.forEach(print);
|
||||
}
|
||||
31
Task/Parametric-polymorphism/E/parametric-polymorphism-1.e
Normal file
31
Task/Parametric-polymorphism/E/parametric-polymorphism-1.e
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
interface TreeAny guards TreeStamp {}
|
||||
def Tree {
|
||||
to get(Value) {
|
||||
def Tree1 {
|
||||
to coerce(specimen, ejector) {
|
||||
def tree := TreeAny.coerce(specimen, ejector)
|
||||
if (tree.valueType() != Value) {
|
||||
throw.eject(ejector, "Tree value type mismatch")
|
||||
}
|
||||
return tree
|
||||
}
|
||||
}
|
||||
return Tree1
|
||||
}
|
||||
}
|
||||
|
||||
def makeTree(T, var value :T, left :nullOk[Tree[T]], right :nullOk[Tree[T]]) {
|
||||
def tree implements TreeStamp {
|
||||
to valueType() { return T }
|
||||
to map(f) {
|
||||
value := f(value) # the declaration of value causes this to be checked
|
||||
if (left != null) {
|
||||
left.map(f)
|
||||
}
|
||||
if (right != null) {
|
||||
right.map(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
return tree
|
||||
}
|
||||
11
Task/Parametric-polymorphism/E/parametric-polymorphism-2.e
Normal file
11
Task/Parametric-polymorphism/E/parametric-polymorphism-2.e
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
? def t := makeTree(int, 0, null, null)
|
||||
# value: <tree>
|
||||
|
||||
? t :Tree[String]
|
||||
# problem: Tree value type mismatch
|
||||
|
||||
? t :Tree[Int]
|
||||
# problem: Failed: Undefined variable: Int
|
||||
|
||||
? t :Tree[int]
|
||||
# value: <tree>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
namespace RosettaCode
|
||||
|
||||
type BinaryTree<'T> =
|
||||
| Element of 'T
|
||||
| Tree of 'T * BinaryTree<'T> * BinaryTree<'T>
|
||||
member this.Map(f) =
|
||||
match this with
|
||||
| Element(x) -> Element(f x)
|
||||
| Tree(x,left,right) -> Tree((f x), left.Map(f), right.Map(f))
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let t1 = Tree(2, Element(1), Tree(4,Element(3),Element(5)) )
|
||||
let t2 = t1.Map(fun x -> x * 10)
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
MODULE SORTSEARCH !Genuflect towards Prof. D. Knuth.
|
||||
|
||||
INTERFACE FIND !Binary chop search, not indexed.
|
||||
MODULE PROCEDURE
|
||||
1 FINDI4, !I: of integers.
|
||||
2 FINDF4,FINDF8, !F: of numbers.
|
||||
3 FINDTTI2,FINDTTI4 !T: of texts.
|
||||
END INTERFACE FIND
|
||||
|
||||
CONTAINS
|
||||
INTEGER FUNCTION FINDI4(THIS,NUMB,N) !Binary chopper. Find i such that THIS = NUMB(i)
|
||||
USE ASSISTANCE !Only for the trace stuff.
|
||||
INTENT(IN) THIS,NUMB,N !Imply read-only, but definitely no need for any "copy-back".
|
||||
INTEGER*4 THIS,NUMB(1:*) !Where is THIS in array NUMB(1:N)?
|
||||
INTEGER N !The count. In other versions, it is supplied by the index.
|
||||
INTEGER L,R,P !Fingers.
|
||||
Chop away.
|
||||
L = 0 !Establish outer bounds.
|
||||
R = N + 1 !One before, and one after, the first and last.
|
||||
1 P = (R - L)/2 !Probe point offset. Beware integer overflow with (L + R)/2.
|
||||
IF (P.LE.0) THEN !Aha! Nowhere! And THIS follows NUMB(L).
|
||||
FINDI4 = -L !Having -L rather than 0 (or other code) might be of interest.
|
||||
RETURN !Finished.
|
||||
END IF !So much for exhaustion.
|
||||
P = P + L !Convert from offset to probe point.
|
||||
IF (THIS - NUMB(P)) 3,4,2 !Compare to the probe point.
|
||||
2 L = P !Shift the left bound up: THIS follows NUMB(P).
|
||||
GO TO 1 !Another chop.
|
||||
3 R = P !Shift the right bound down: THIS precedes NUMB(P).
|
||||
GO TO 1 !Try again.
|
||||
Caught it! THIS = NUMB(P)
|
||||
4 FINDI4 = P !So, THIS is found, here!
|
||||
END FUNCTION FINDI4 !On success, THIS = NUMB(FINDI4); no fancy index here...
|
||||
|
||||
END MODULE SORTSEARCH
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
TYPE STUFF
|
||||
INTEGER CODE !A key number.
|
||||
CHARACTER*6 NAME !Associated data.
|
||||
INTEGER THIS !etc.
|
||||
END TYPE STUFF
|
||||
TYPE(STUFF) TABLE(600) !An array of such entries.
|
||||
53
Task/Parametric-polymorphism/Go/parametric-polymorphism-1.go
Normal file
53
Task/Parametric-polymorphism/Go/parametric-polymorphism-1.go
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func average(c intCollection) float64 {
|
||||
var sum, count int
|
||||
c.mapElements(func(n int) {
|
||||
sum += n
|
||||
count++
|
||||
})
|
||||
return float64(sum) / float64(count)
|
||||
}
|
||||
|
||||
func main() {
|
||||
t1 := new(binaryTree)
|
||||
t2 := new(bTree)
|
||||
a1 := average(t1)
|
||||
a2 := average(t2)
|
||||
fmt.Println("binary tree average:", a1)
|
||||
fmt.Println("b-tree average:", a2)
|
||||
}
|
||||
|
||||
type intCollection interface {
|
||||
mapElements(func(int))
|
||||
}
|
||||
|
||||
type binaryTree struct {
|
||||
// dummy representation details
|
||||
left, right bool
|
||||
}
|
||||
|
||||
func (t *binaryTree) mapElements(visit func(int)) {
|
||||
// dummy implementation
|
||||
if t.left == t.right {
|
||||
visit(3)
|
||||
visit(1)
|
||||
visit(4)
|
||||
}
|
||||
}
|
||||
|
||||
type bTree struct {
|
||||
// dummy representation details
|
||||
buckets int
|
||||
}
|
||||
|
||||
func (t *bTree) mapElements(visit func(int)) {
|
||||
// dummy implementation
|
||||
if t.buckets >= 0 {
|
||||
visit(1)
|
||||
visit(5)
|
||||
visit(9)
|
||||
}
|
||||
}
|
||||
13
Task/Parametric-polymorphism/Go/parametric-polymorphism-2.go
Normal file
13
Task/Parametric-polymorphism/Go/parametric-polymorphism-2.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package rosettacode
|
||||
|
||||
type Tree(type T) struct {
|
||||
val T
|
||||
left *Tree(T)
|
||||
right *Tree(T)
|
||||
}
|
||||
|
||||
func (t *Tree(T)) ReplaceAll(rep T) {
|
||||
t.val = rep
|
||||
if t.left != nil { t.left.ReplaceAll(rep) }
|
||||
if t.right != nil { t.right.ReplaceAll(rep) }
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
class Tree<T> {
|
||||
T value
|
||||
Tree<T> left
|
||||
Tree<T> right
|
||||
|
||||
Tree(T value = null, Tree<T> left = null, Tree<T> right = null) {
|
||||
this.value = value
|
||||
this.left = left
|
||||
this.right = right
|
||||
}
|
||||
|
||||
void replaceAll(T value) {
|
||||
this.value = value
|
||||
left?.replaceAll(value)
|
||||
right?.replaceAll(value)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
data Tree a = Empty | Node a (Tree a) (Tree a)
|
||||
|
||||
mapTree :: (a -> b) -> Tree a -> Tree b
|
||||
mapTree f Empty = Empty
|
||||
mapTree f (Node x l r) = Node (f x) (mapTree f l) (mapTree f r)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
instance Functor Tree where
|
||||
fmap f Empty = Empty
|
||||
fmap f (Node x l r) = Node (f x) (fmap f l) (fmap f r)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
add1Everywhere :: (Functor f, Num a) => f a -> f a
|
||||
add1Everywhere nums = fmap (\x -> x + 1) nums
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
procedure main()
|
||||
bTree := [1, [2, [4, [7]], [5]], [3, [6, [8], [9]]]]
|
||||
mapTree(bTree, write)
|
||||
bTree := [1, ["two", ["four", [7]], [5]], [3, ["six", ["eight"], [9]]]]
|
||||
mapTree(bTree, write)
|
||||
end
|
||||
|
||||
procedure mapTree(tree, f)
|
||||
every f(\tree[1]) | mapTree(!tree[2:0], f)
|
||||
end
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
Polymorphism is a room.
|
||||
|
||||
To find (V - K) in (L - list of values of kind K):
|
||||
repeat with N running from 1 to the number of entries in L:
|
||||
if entry N in L is V:
|
||||
say "Found [V] at entry [N] in [L].";
|
||||
stop;
|
||||
say "Did not find [V] in [L]."
|
||||
|
||||
When play begins:
|
||||
find "needle" in {"parrot", "needle", "rutabaga"};
|
||||
find 6 in {2, 3, 4};
|
||||
end the story.
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
list of numbers
|
||||
relation of texts to rooms
|
||||
object based rulebook producing a number
|
||||
description of things
|
||||
activity on things
|
||||
number valued property
|
||||
text valued table column
|
||||
phrase (text, text) -> number
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
public class Tree<T>{
|
||||
private T value;
|
||||
private Tree<T> left;
|
||||
private Tree<T> right;
|
||||
|
||||
public void replaceAll(T value){
|
||||
this.value = value;
|
||||
if (left != null)
|
||||
left.replaceAll(value);
|
||||
if (right != null)
|
||||
right.replaceAll(value);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
module BinaryTrees
|
||||
|
||||
mutable struct BinaryTree{V}
|
||||
v::V
|
||||
l::Union{BinaryTree{V}, Nothing}
|
||||
r::Union{BinaryTree{V}, Nothing}
|
||||
end
|
||||
|
||||
BinaryTree(v) = BinaryTree(v, nothing, nothing)
|
||||
|
||||
map(f, bt::BinaryTree) = BinaryTree(f(bt.v), map(f, bt.l), map(f, bt.r))
|
||||
map(f, bt::Nothing) = nothing
|
||||
|
||||
let inttree = BinaryTree(
|
||||
0,
|
||||
BinaryTree(
|
||||
1,
|
||||
BinaryTree(3),
|
||||
BinaryTree(5),
|
||||
),
|
||||
BinaryTree(
|
||||
2,
|
||||
BinaryTree(4),
|
||||
nothing,
|
||||
),
|
||||
)
|
||||
map(x -> 2x^2, inttree)
|
||||
end
|
||||
|
||||
let strtree = BinaryTree(
|
||||
"hello",
|
||||
BinaryTree(
|
||||
"world!",
|
||||
BinaryTree("Julia"),
|
||||
nothing,
|
||||
),
|
||||
BinaryTree(
|
||||
"foo",
|
||||
BinaryTree("bar"),
|
||||
BinaryTree("baz"),
|
||||
),
|
||||
)
|
||||
map(uppercase, strtree)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
// version 1.0.6
|
||||
|
||||
class BinaryTree<T>(var value: T) {
|
||||
var left : BinaryTree<T>? = null
|
||||
var right: BinaryTree<T>? = null
|
||||
|
||||
fun <U> map(f: (T) -> U): BinaryTree<U> {
|
||||
val tree = BinaryTree<U>(f(value))
|
||||
if (left != null) tree.left = left?.map(f)
|
||||
if (right != null) tree.right = right?.map(f)
|
||||
return tree
|
||||
}
|
||||
|
||||
fun showTopThree() = "(${left?.value}, $value, ${right?.value})"
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val b = BinaryTree(6)
|
||||
b.left = BinaryTree(5)
|
||||
b.right = BinaryTree(7)
|
||||
println(b.showTopThree())
|
||||
val b2 = b.map { it * 10.0 }
|
||||
println(b2.showTopThree())
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
f[a_] := Join[a, a]
|
||||
f[{1, 2, 3}]
|
||||
f[{"1", "2", "3"}]
|
||||
f[{1.1, 2.1, 3.1}]
|
||||
f[G[1, "a", Pi]]
|
||||
g[x_] := x^2
|
||||
g[2]
|
||||
g[3.5]
|
||||
g[Pi]
|
||||
g["a"]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
:- type tree(A) ---> empty ; node(A, tree(A), tree(A)).
|
||||
|
||||
:- func map(func(A) = B, tree(A)) = tree(B).
|
||||
|
||||
map(_, empty) = empty.
|
||||
map(F, node(A, Left, Right)) = node(F(A), map(F, Left), map(F, Right)).
|
||||
48
Task/Parametric-polymorphism/Nim/parametric-polymorphism.nim
Normal file
48
Task/Parametric-polymorphism/Nim/parametric-polymorphism.nim
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import strutils, sugar
|
||||
|
||||
type Tree[T] = ref object
|
||||
value: T
|
||||
left, right: Tree[T]
|
||||
|
||||
|
||||
proc newTree[T](value = default(T)): Tree[T] =
|
||||
## Create a tree with a single node with the given value.
|
||||
Tree[T](value: value)
|
||||
|
||||
|
||||
proc map[T, U](tree: Tree[T]; f: (T) -> U): Tree[U] =
|
||||
## Apply function "f" to each element of a tree, building
|
||||
## another tree.
|
||||
result = newTree[U](f(tree.value))
|
||||
if not tree.left.isNil:
|
||||
result.left = tree.left.map(f)
|
||||
if not tree.right.isNil:
|
||||
result.right = tree.right.map(f)
|
||||
|
||||
|
||||
proc print(tree: Tree; indent = 0) =
|
||||
## Print a tree.
|
||||
let start = repeat(' ', indent)
|
||||
echo start, "value: ", tree.value
|
||||
if tree.left.isNil:
|
||||
echo start, " nil"
|
||||
else:
|
||||
print(tree.left, indent + 2)
|
||||
if tree.right.isNil:
|
||||
echo start, " nil"
|
||||
else:
|
||||
print(tree.right, indent + 2)
|
||||
|
||||
|
||||
when isMainModule:
|
||||
|
||||
echo "Initial tree:"
|
||||
var tree = newTree[int](5)
|
||||
tree.left = newTree[int](2)
|
||||
tree.right = newTree[int](7)
|
||||
print(tree)
|
||||
|
||||
echo ""
|
||||
echo "Tree created by applying a function to each node:"
|
||||
let tree1 = tree.map((x) => 1 / x)
|
||||
print(tree1)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
type 'a tree = Empty | Node of 'a * 'a tree * 'a tree
|
||||
|
||||
(** val map_tree : ('a -> 'b) -> 'a tree -> 'b tree *)
|
||||
let rec map_tree f = function
|
||||
| Empty -> Empty
|
||||
| Node (x,l,r) -> Node (f x, map_tree f l, map_tree f r)
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
@interface Tree<T> : NSObject {
|
||||
T value;
|
||||
Tree<T> *left;
|
||||
Tree<T> *right;
|
||||
}
|
||||
|
||||
- (void)replaceAll:(T)v;
|
||||
@end
|
||||
|
||||
@implementation Tree
|
||||
- (void)replaceAll:(id)v {
|
||||
value = v;
|
||||
[left replaceAll:v];
|
||||
[right replaceAll:v];
|
||||
}
|
||||
@end
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shuffle</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"oranges"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"apples"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">}))</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">enum</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">right</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">data</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">data</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]!=</span><span style="color: #004600;">null</span> <span style="color: #008080;">then</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]!=</span><span style="color: #004600;">null</span> <span style="color: #008080;">then</span> <span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tree</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">tree</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">v</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">v</span><span style="color: #0000FF;">,</span><span style="color: #004600;">null</span><span style="color: #0000FF;">,</span><span style="color: #004600;">null</span><span style="color: #0000FF;">}</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">add10</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.00</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- Add some nodes.</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">][</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.11</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">left</span><span style="color: #0000FF;">][</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.12</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.20</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">][</span><span style="color: #000000;">left</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.21</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">root</span><span style="color: #0000FF;">[</span><span style="color: #000000;">right</span><span style="color: #0000FF;">][</span><span style="color: #000000;">right</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">newnode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1.22</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Now the tree has seven nodes.
|
||||
|
||||
-- Show the whole tree.</span>
|
||||
<span style="color: #7060A8;">ppOpt</span><span style="color: #0000FF;">({</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Modify the whole tree.</span>
|
||||
<span style="color: #000000;">root</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">,</span><span style="color: #000000;">add10</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Create a whole new tree.</span>
|
||||
<span style="color: #004080;">object</span> <span style="color: #000000;">root2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tmap</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">newnode</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Show the whole tree again.</span>
|
||||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">root</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(de mapTree (Tree Fun)
|
||||
(set Tree (Fun (car Tree)))
|
||||
(and (cadr Tree) (mapTree @ Fun))
|
||||
(and (cddr Tree) (mapTree @ Fun)) )
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*REXX program demonstrates (with displays) a method of parametric polymorphism. */
|
||||
call newRoot 1.00, 3 /*new root, and also indicate 3 stems.*/
|
||||
/* [↓] no need to label the stems. */
|
||||
call addStem 1.10 /*a new stem and its initial value. */
|
||||
call addStem 1.11 /*" " " " " " " */
|
||||
call addStem 1.12 /*" " " " " " " */
|
||||
call addStem 1.20 /*" " " " " " " */
|
||||
call addStem 1.21 /*" " " " " " " */
|
||||
call addStem 1.22 /*" " " " " " " */
|
||||
call sayNodes /*display some nicely formatted values.*/
|
||||
call modRoot 50 /*modRoot will add fifty to all stems. */
|
||||
call sayNodes /*display some nicely formatted values.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
addStem: nodes= nodes + 1; do j=1 for stems; root.nodes.j= arg(1); end; return
|
||||
newRoot: parse arg @,stems; nodes= -1; call addStem copies('═',9); call addStem @; return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
modRoot: arg #; do j=1 for nodes /*traipse through all the defined nodes*/
|
||||
do k=1 for stems /*add bias ──►───────────────────────┐ */
|
||||
if datatype(root.j.k, 'N') then root.j.k= root.j.k + # /* ◄───┘ */
|
||||
end /*k*/ /* [↑] add if stem value is numeric.*/
|
||||
end /*j*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sayNodes: w= 9; do j=0 to nodes; _= /*ensure each of the nodes gets shown. */
|
||||
do k=1 for stems; _= _ center(root.j.k, w) /*concatenate a node.*/
|
||||
end /*k*/
|
||||
$= word('node='j, 1 + (j<1) ) /*define a label for this line's output*/
|
||||
say center($, w) substr(_, 2) /*ignore 1st (leading) blank which was */
|
||||
end /*j*/ /* [↑] caused by concatenation.*/
|
||||
say /*show a blank line to separate outputs*/
|
||||
return
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
#lang typed/racket
|
||||
|
||||
(define-type (Tree A) (U False (Node A)))
|
||||
|
||||
(struct: (A) Node
|
||||
([val : A] [left : (Tree A)] [right : (Tree A)])
|
||||
#:transparent)
|
||||
|
||||
(: tree-map (All (A B) (A -> B) (Tree A) -> (Tree B)))
|
||||
(define (tree-map f tree)
|
||||
(match tree
|
||||
[#f #f]
|
||||
[(Node val left right)
|
||||
(Node (f val) (tree-map f left) (tree-map f right))]))
|
||||
|
||||
;; unit tests
|
||||
(require typed/rackunit)
|
||||
(check-equal?
|
||||
(tree-map add1 (Node 5 (Node 3 #f #f) #f))
|
||||
(Node 6 (Node 4 #f #f) #f))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
role BinaryTree[::T] {
|
||||
has T $.value;
|
||||
has BinaryTree[T] $.left;
|
||||
has BinaryTree[T] $.right;
|
||||
|
||||
method replace-all(T $value) {
|
||||
$!value = $value;
|
||||
$!left.replace-all($value) if $!left.defined;
|
||||
$!right.replace-all($value) if $!right.defined;
|
||||
}
|
||||
}
|
||||
|
||||
class IntTree does BinaryTree[Int] { }
|
||||
|
||||
my IntTree $it .= new(value => 1,
|
||||
left => IntTree.new(value => 2),
|
||||
right => IntTree.new(value => 3));
|
||||
|
||||
$it.replace-all(42);
|
||||
say $it;
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
struct TreeNode<T> {
|
||||
value: T,
|
||||
left: Option<Box<TreeNode<T>>>,
|
||||
right: Option<Box<TreeNode<T>>>,
|
||||
}
|
||||
|
||||
impl <T> TreeNode<T> {
|
||||
fn my_map<U,F>(&self, f: &F) -> TreeNode<U> where
|
||||
F: Fn(&T) -> U {
|
||||
TreeNode {
|
||||
value: f(&self.value),
|
||||
left: match self.left {
|
||||
None => None,
|
||||
Some(ref n) => Some(Box::new(n.my_map(f))),
|
||||
},
|
||||
right: match self.right {
|
||||
None => None,
|
||||
Some(ref n) => Some(Box::new(n.my_map(f))),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let root = TreeNode {
|
||||
value: 3,
|
||||
left: Some(Box::new(TreeNode {
|
||||
value: 55,
|
||||
left: None,
|
||||
right: None,
|
||||
})),
|
||||
right: Some(Box::new(TreeNode {
|
||||
value: 234,
|
||||
left: Some(Box::new(TreeNode {
|
||||
value: 0,
|
||||
left: None,
|
||||
right: None,
|
||||
})),
|
||||
right: None,
|
||||
})),
|
||||
};
|
||||
root.my_map(&|x| { println!("{}" , x)});
|
||||
println!("---------------");
|
||||
let new_root = root.my_map(&|x| *x as f64 * 333.333f64);
|
||||
new_root.my_map(&|x| { println!("{}" , x) });
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
case class Tree[+A](value: A, left: Option[Tree[A]], right: Option[Tree[A]]) {
|
||||
def map[B](f: A => B): Tree[B] =
|
||||
Tree(f(value), left map (_.map(f)), right map (_.map(f)))
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
class Employee(val name: String)
|
||||
class Manager(name: String) extends Employee(name)
|
||||
|
||||
val t = Tree(new Manager("PHB"), None, None)
|
||||
val t2: Tree[Employee] = t
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def toName(e: Employee) = e.name
|
||||
val treeOfNames = t.map(toName)
|
||||
|
|
@ -0,0 +1 @@
|
|||
trait Function1[-T1, +R]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
case class Tree[+A](value: A, left: Option[Tree[A]], right: Option[Tree[A]]) {
|
||||
def map[B](f: A => B): Tree[B] =
|
||||
Tree(f(value), left map (_.map(f)), right map (_.map(f)))
|
||||
def find[B >: A](what: B): Boolean =
|
||||
(value == what) || left.map(_.find(what)).getOrElse(false) || right.map(_.find(what)).getOrElse(false)
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
if (t2.find(new Employee("Dilbert")))
|
||||
println("Call Catbert!")
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
trait DFA {
|
||||
type Element
|
||||
val map = new collection.mutable.HashMap[Element, DFA]()
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func type: container (in type: elemType) is func
|
||||
result
|
||||
var type: container is void;
|
||||
begin
|
||||
container := array elemType;
|
||||
|
||||
global
|
||||
|
||||
const func container: map (in container: aContainer,
|
||||
inout elemType: aVariable, ref func elemType: aFunc) is func
|
||||
result
|
||||
var container: mapResult is container.value;
|
||||
begin
|
||||
for aVariable range aContainer do
|
||||
mapResult &:= aFunc;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
end global;
|
||||
end func;
|
||||
|
||||
const type: intContainer is container(integer);
|
||||
var intContainer: container1 is [] (1, 2, 4, 6, 10, 12, 16, 18, 22);
|
||||
var intContainer: container2 is 0 times 0;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: num is 0;
|
||||
begin
|
||||
container2 := map(container1, num, num + 1);
|
||||
for num range container2 do
|
||||
write(num <& " ");
|
||||
end for;
|
||||
writeln;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
datatype 'a tree = Empty | Node of 'a * 'a tree * 'a tree
|
||||
|
||||
(** val map_tree = fn : ('a -> 'b) -> 'a tree -> 'b tree *)
|
||||
fun map_tree f Empty = Empty
|
||||
| map_tree f (Node (x,l,r)) = Node (f x, map_tree f l, map_tree f r)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
class Tree<T> {
|
||||
var value: T?
|
||||
var left: Tree<T>?
|
||||
var right: Tree<T>?
|
||||
|
||||
func replaceAll(value: T?) {
|
||||
self.value = value
|
||||
left?.replaceAll(value)
|
||||
right?.replaceAll(value)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
enum Tree<T> {
|
||||
case Empty
|
||||
indirect case Node(T, Tree<T>, Tree<T>)
|
||||
|
||||
func map<U>(f : T -> U) -> Tree<U> {
|
||||
switch(self) {
|
||||
case .Empty : return .Empty
|
||||
case let .Node(x, l, r): return .Node(f(x), l.map(f), r.map(f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
binary_tree_of "node-type" = "node-type"%hhhhWZAZ
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#import tag
|
||||
|
||||
#fix general_type_fixer 1
|
||||
|
||||
binary_tree_of "node-type" = ("node-type",(binary_tree_of "node-type")%Z)%drWZwlwAZ
|
||||
|
|
@ -0,0 +1 @@
|
|||
binary_tree_of = %-hhhhWZAZ
|
||||
|
|
@ -0,0 +1 @@
|
|||
binary_tree_map "f" = ~&a^& ^A/"f"@an ~&amPfamPWB
|
||||
|
|
@ -0,0 +1 @@
|
|||
binary_tree_map = ~&a^&+ ^A\~&amPfamPWB+ @an
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
string_tree = binary_tree_of %s
|
||||
|
||||
x = 'foo': ('bar': (),'baz': ())
|
||||
|
||||
#cast string_tree
|
||||
|
||||
example = (binary_tree_map "s". "s"--"s") x
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
Class BinaryTree(Of T)
|
||||
ReadOnly Property Left As BinaryTree(Of T)
|
||||
ReadOnly Property Right As BinaryTree(Of T)
|
||||
ReadOnly Property Value As T
|
||||
|
||||
Sub New(value As T, Optional left As BinaryTree(Of T) = Nothing, Optional right As BinaryTree(Of T) = Nothing)
|
||||
Me.Value = value
|
||||
Me.Left = left
|
||||
Me.Right = right
|
||||
End Sub
|
||||
|
||||
Function Map(Of U)(f As Func(Of T, U)) As BinaryTree(Of U)
|
||||
Return New BinaryTree(Of U)(f(Me.Value), Me.Left?.Map(f), Me.Right?.Map(f))
|
||||
End Function
|
||||
|
||||
Overrides Function ToString() As String
|
||||
Dim sb As New Text.StringBuilder()
|
||||
Me.ToString(sb, 0)
|
||||
Return sb.ToString()
|
||||
End Function
|
||||
|
||||
Private Overloads Sub ToString(sb As Text.StringBuilder, depth As Integer)
|
||||
sb.Append(New String(ChrW(AscW(vbTab)), depth))
|
||||
sb.AppendLine(Me.Value?.ToString())
|
||||
Me.Left?.ToString(sb, depth + 1)
|
||||
Me.Right?.ToString(sb, depth + 1)
|
||||
End Sub
|
||||
End Class
|
||||
|
||||
Module Program
|
||||
Sub Main()
|
||||
Dim b As New BinaryTree(Of Integer)(6, New BinaryTree(Of Integer)(5), New BinaryTree(Of Integer)(7))
|
||||
Dim b2 As BinaryTree(Of Double) = b.Map(Function(x) x * 0.5)
|
||||
|
||||
Console.WriteLine(b)
|
||||
Console.WriteLine(b2)
|
||||
End Sub
|
||||
End Module
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
domains
|
||||
tree{Type} = branch(tree{Type} Left, tree{Type} Right); leaf(Type Value).
|
||||
|
||||
class predicates
|
||||
treewalk : (tree{X},function{X,Y}) -> tree{Y} procedure (i,i).
|
||||
|
||||
clauses
|
||||
treewalk(branch(Left,Right),Func) = branch(NewLeft,NewRight) :-
|
||||
NewLeft = treewalk(Left,Func), NewRight = treewalk(Right,Func).
|
||||
|
||||
treewalk(leaf(Value),Func) = leaf(X) :-
|
||||
X = Func(Value).
|
||||
|
||||
run():-
|
||||
init(),
|
||||
X = branch(leaf(2), branch(leaf(3),leaf(4))),
|
||||
Y = treewalk(X,addone),
|
||||
write(Y),
|
||||
succeed().
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
class BinaryTree {
|
||||
construct new(T, value) {
|
||||
if (!(T is Class)) Fiber.abort ("T must be a class.")
|
||||
if (value.type != T) Fiber.abort("Value must be of type T.")
|
||||
_kind = T
|
||||
_value = value
|
||||
_left = null
|
||||
_right = null
|
||||
}
|
||||
|
||||
// constructor overload to enable kind to be inferred from type of value
|
||||
static new (value) { new(value.type, value) }
|
||||
|
||||
kind { _kind }
|
||||
value { _value}
|
||||
value=(v) {
|
||||
if (v.type != _kind) Fiber.abort("Value must be of type %(_kind)")
|
||||
_value = v
|
||||
}
|
||||
|
||||
left { _left }
|
||||
right { _right }
|
||||
left=(b) {
|
||||
if (b.type != BinaryTree || b.kind != _kind) {
|
||||
Fiber.abort("Argument must be a BinaryTree of type %(_kind)")
|
||||
}
|
||||
_left = b
|
||||
}
|
||||
right=(b) {
|
||||
if (b.type != BinaryTree || b.kind != _kind) {
|
||||
Fiber.abort("Argument must be a BinaryTree of type %(_kind)")
|
||||
}
|
||||
_right = b
|
||||
}
|
||||
|
||||
map(f) {
|
||||
var tree = BinaryTree.new(f.call(_value))
|
||||
if (_left) tree.left = left.map(f)
|
||||
if (_right) tree.right = right.map(f)
|
||||
return tree
|
||||
}
|
||||
|
||||
showTopThree() { "(%(left.value), %(value), %(right.value))" }
|
||||
}
|
||||
|
||||
var b = BinaryTree.new(6)
|
||||
b.left = BinaryTree.new(5)
|
||||
b.right = BinaryTree.new(7)
|
||||
System.print(b.showTopThree())
|
||||
|
||||
var b2 = b.map{ |i| i * 10 }
|
||||
System.print(b2.showTopThree())
|
||||
b2.value = "six" // generates an error because "six" is not a Num
|
||||
Loading…
Add table
Add a link
Reference in a new issue