Skip to content

Commit af3c4ad

Browse files
committed
Implement composed2 and composed3 functions
1 parent 6f17a06 commit af3c4ad

File tree

3 files changed

+89
-5
lines changed

3 files changed

+89
-5
lines changed

QSharpMathUtils/src/HigherOrderFunctions.qs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
namespace HigherOrderFunctions {
22

3+
function Composed2<'T, 'U, 'V>(f1 : 'U -> 'V, f2 : 'T -> 'U) : ('T -> 'V) {
4+
x -> f1(f2(x))
5+
}
6+
7+
function Composed3<'T, 'U, 'V, 'W>(f1 : 'V -> 'W, f2 : 'U -> 'V, f3 : 'T -> 'U) : ('T -> 'W) {
8+
x -> f1(f2(f3(x)))
9+
}
10+
311
function Curry2<'T, 'U, 'V>(f : ('T, 'U) -> 'V) : ('T -> 'U -> 'V) {
412
x -> y -> f(x, y)
513
}
@@ -16,6 +24,6 @@ namespace HigherOrderFunctions {
1624
y -> x -> f(x)(y)
1725
}
1826

19-
export Curry2, Curry3, Flip2, Flip2_;
27+
export Composed2, Composed3, Curry2, Curry3, Flip2, Flip2_;
2028

2129
}

QSharpMathUtils/src/Main.qs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import Matrices.Identity.*;
1111
import Matrices.Multiply.*;
1212
import Complex.*;
1313
import Categories.Monoid.*;
14+
import HigherOrderFunctions.*;
1415

1516
@EntryPoint()
16-
function Main() : Double[][] {
17+
function Main() : Int {
18+
let add : (Int, Int) -> Int = (x, y) -> x + y;
19+
let mul3 : Int -> Int = x -> x * 3;
1720

18-
let matrix = [[1., 2.], [3., 4.]];
19-
let matrix2 = [[-1., 3./2.], [1., -1.]];
20-
MatMulD(matrix2, Invert2x2D(matrix2))
21+
let f = Composed2(add(1, _), mul3);
22+
f(1)
2123

2224
}
2325

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import qsharp # type: ignore
2+
from .setup import init_qsharp
3+
4+
init_qsharp()
5+
6+
def test_compose2_normal():
7+
# Arrange
8+
qscode = ("import Std.Math.*;"
9+
"import HigherOrderFunctions.*;"
10+
"let add1 : Int -> Int = x -> x + 1;"
11+
"let mul3 : Int -> Int = x -> x * 3;"
12+
"Composed2(add1, mul3)(1)")
13+
14+
# Act
15+
result = qsharp.eval(qscode)
16+
17+
# Assert
18+
assert result == 4
19+
20+
21+
def test_compose2_partial():
22+
# Arrange
23+
qscode = ("import Std.Math.*;"
24+
"import HigherOrderFunctions.*;"
25+
"let add : (Int, Int) -> Int = (x, y) -> x + y;"
26+
"let mul3 : Int -> Int = x -> x * 3;"
27+
"Composed2(add(1, _), mul3)(1)")
28+
29+
# Act
30+
result = qsharp.eval(qscode)
31+
32+
# Assert
33+
assert result == 4
34+
35+
36+
def test_compose3_normal():
37+
# Arrange
38+
qscode = ("import Std.Math.*;"
39+
"import HigherOrderFunctions.*;"
40+
"let add1 : Int -> Int = x -> x + 1;"
41+
"let mul3 : Int -> Int = x -> x * 3;"
42+
"let minus1 : Int -> Int = x -> x - 1;"
43+
"Composed3(minus1, add1, mul3)(1)")
44+
45+
# Act
46+
result = qsharp.eval(qscode)
47+
48+
# Assert
49+
assert result == 3
50+
51+
52+
def test_complex_equal_sad():
53+
# Arrange
54+
qscode = ("import Std.Math.*;"
55+
"import Complex.*;"
56+
"EqualC(Complex(1., 0.), Complex(1., 1.));")
57+
58+
# Act
59+
result = qsharp.eval(qscode)
60+
61+
# Assert
62+
assert result == False
63+
64+
65+
def test_to_complex():
66+
# Arrange
67+
qscode = ("import Complex.*;"
68+
"ToC(1.3)")
69+
70+
# Act
71+
result = qsharp.eval(qscode)
72+
73+
# Assert
74+
assert result == (1.3, 0.0)

0 commit comments

Comments
 (0)