Skip to content

Commit

Permalink
Merge pull request #102 from corywalker/corywalker
Browse files Browse the repository at this point in the history
Corywalker
  • Loading branch information
corywalker committed Sep 8, 2017
2 parents 32ed3cf + 8c66b05 commit 011f1b0
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
expreduce/resources.go linguist-generated=true
14 changes: 14 additions & 0 deletions expreduce/builtin_flowcontrol.go
Expand Up @@ -128,6 +128,20 @@ func GetFlowControlDefinitions() (defs []Definition) {
return &Symbol{"System`Null"}
},
})
defs = append(defs, Definition{
Name: "Switch",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) < 4 || len(this.Parts) % 2 != 0 {
return this
}
for i := 2; i < len(this.Parts); i += 2 {
if match, _ := IsMatchQ(this.Parts[1], this.Parts[i], EmptyPD(), es); match {
return this.Parts[i+1]
}
}
return this
},
})
defs = append(defs, Definition{
Name: "With",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
Expand Down
4 changes: 4 additions & 0 deletions expreduce/builtin_list.go
Expand Up @@ -578,5 +578,9 @@ func GetListDefinitions() (defs []Definition) {
defs = append(defs, Definition{Name: "Last"})
defs = append(defs, Definition{Name: "First"})
defs = append(defs, Definition{Name: "Rest"})
defs = append(defs, Definition{
Name: "ReplacePart",
OmitDocumentation: true,
})
return
}
4 changes: 4 additions & 0 deletions expreduce/builtin_numbertheory.go
Expand Up @@ -146,5 +146,9 @@ func GetNumberTheoryDefinitions() (defs []Definition) {
})*/
defs = append(defs, Definition{Name: "EvenQ"})
defs = append(defs, Definition{Name: "OddQ"})
defs = append(defs, Definition{
Name: "FactorInteger",
OmitDocumentation: true,
})
return
}
12 changes: 6 additions & 6 deletions expreduce/resources.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions expreduce/resources/flowcontrol.m
Expand Up @@ -71,6 +71,20 @@
]
};

Switch::usage = "`Switch[e, case1, val1, case2, val2, ...]` attempts to match `e` with the cases in order. If a match is found, returns the corresponding value..";
Attributes[Switch] = {HoldRest, Protected, ReadProtected};
Tests`Switch = {
ESimpleExamples[
ESameTest[b, Switch[z,_,b,z,c]],
ESameTest[k, Switch[z,k_Symbol,k]],
ESameTest[Switch[z,1], Switch[z,1]],
ESameTest[Switch[z,d,b,l,c], Switch[z,d,b,l,c]]
], ETests[
ESameTest[Switch[], Switch[]],
ESameTest[Switch[z], Switch[z]]
]
};

With::usage = "`With[{s1=v1, s2=v2, ...}, body]` locally replaces the specified symbols in body with their respective values.";
Attributes[With] = {HoldAll, Protected};
Tests`With = {
Expand Down
7 changes: 7 additions & 0 deletions expreduce/resources/list.m
Expand Up @@ -378,3 +378,10 @@
ESameTest[foo[a,b,c], Join[foo[a],foo[b,c]]]
]
};

ReplacePart[e_?((! AtomQ[#]) &), r_, i_Integer?Positive] :=

If[i <= Length[e] === True,
Join[e[[1 ;; i - 1]], Head[e][r], e[[i + 1 ;; Length[e]]]],
Print["Index too large for ReplacePart!"]];
ReplacePart[___] := Print["Invalid call to ReplacePart!"];
8 changes: 8 additions & 0 deletions expreduce/resources/numbertheory.m
Expand Up @@ -137,3 +137,11 @@
ESameTest[OddQ[a], OddQ[a]]
]
};

FactorInteger[n_] := Switch[n,
-20, {{-1,1},{2,2},{5,1}},
1, {{1, 1}},
2, {{2, 1}},
3, {{3, 1}},
Rational[21,4], {{2, -2}, {3, 1}, {7, 1}},
_, Print["Invalid call to FactorInteger!", n]];

0 comments on commit 011f1b0

Please sign in to comment.