From d07bd2a7b586c00a30e66d5ab57ea79385936b02 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 29 Jun 2022 08:50:47 +0200 Subject: [PATCH 1/8] Implement basic functions --- .../Standard/Base/0.0.0-dev/src/Function.enso | 55 +++++++++++++++++++ test/Tests/src/Data/Function_Spec.enso | 43 +++++++++++++++ test/Tests/src/Main.enso | 2 + 3 files changed, 100 insertions(+) create mode 100644 test/Tests/src/Data/Function_Spec.enso diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso index 4a478a005f84..0ff13aeb343b 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso @@ -1,3 +1,5 @@ +import Standard.Base + # Function types. type Function @@ -7,3 +9,56 @@ type Function the this argument. @Builtin_Type type Function + + +## An identity function which returns the provided argument. + + Arguments: + - x: the value to return. + + > Example + five = Function.identity 5 # returns number 5 + +Function.identity : a -> a +Function.identity x = x + +## Flips the first two arguments of a function. Returns function that + takes two arguments, but in opposite order. + + Arguments: + - f function that takes two arguments + + > Example + Console.log <| Function.flip (+) "world" "hello" # Prints 'helloworld' + +Function.flip : (a -> b -> c) -> (b -> a -> c) +Function.flip f = (x -> y -> f y x) + + +## Creates a function which drops its input and returns the provided value instead. + The expression const a is the same as \_ -> a. + + Arguments: + - x constant value to return + + > Example + Console.log <| [1, 2, 3].map (Function.const 7) # Prints '[7, 7, 7]' + +Function.const : a -> b -> a +Function.const x _ = x + +## Converts a single-argument function accepting a pair of elements into a multi-argument one. + + Arguments: + - f function accepting pair of values + +Function.curry : ([a, b] -> c) -> (a -> b -> c) +Function.curry f = x -> y -> f [x, y] + +## Converts a multi-argument function into a single-argument one accepting a pair of elements. + + Arguments: + - f function accepting multiple arguments + +Function.uncurry : (a -> b -> c) -> ([a, b] -> c) +Function.uncurry f = (pair -> f pair.head pair.tail.head) diff --git a/test/Tests/src/Data/Function_Spec.enso b/test/Tests/src/Data/Function_Spec.enso new file mode 100644 index 000000000000..ac2cac4600ba --- /dev/null +++ b/test/Tests/src/Data/Function_Spec.enso @@ -0,0 +1,43 @@ +from Standard.Base import all +import Standard.Base.Data.Numbers + +import Standard.Test +from Standard.Base.Data.Ordering import Equal, Less, Greater + +spec = + Test.group "Function.identity" <| + Test.specify "identity on number" <| + (Function.identity 5) . should_equal 5 + + Test.specify "identity on text" <| + (Function.identity '5') . should_equal '5' + + Test.specify "identity on boolean" <| + (Function.identity False) . should_equal False + + Test.group "Function.flip" <| + Test.specify "flip on number" <| + (Function.flip (-) 2 5) . should_equal 3 + + Test.specify "flip on text" <| + (Function.flip (+) "world" "hello") . should_equal "helloworld" + + Test.group "Function.const" <| + Test.specify "const on number" <| + two = Function.const 2 + two 5 . should_equal 2 + + Test.group "Function.curry" <| + Test.specify "curry on number list" <| + sum = x -> x.fold 0 (+) + sum [1, 2, 3, 4] . should_equal 10 + plus = Function.curry sum + plus 6 3 . should_equal 9 + + Test.group "Function.uncurry" <| + Test.specify "uncurry on number list" <| + plus = Function.uncurry (*) + plus [6, 7] . should_equal 42 + + +main = Test.Suite.run_main here.spec diff --git a/test/Tests/src/Main.enso b/test/Tests/src/Main.enso index 8195716994f7..4d679f6a68d4 100644 --- a/test/Tests/src/Main.enso +++ b/test/Tests/src/Main.enso @@ -20,6 +20,7 @@ import project.Semantic.R_Interop_Spec import project.Data.Array_Spec import project.Data.Bool_Spec +import project.Data.Function_Spec import project.Data.Interval_Spec import project.Data.Json_Spec import project.Data.List_Spec @@ -65,6 +66,7 @@ main = Test.Suite.run_main <| Any_Spec.spec Array_Spec.spec Bool_Spec.spec + Function_Spec.spec Case_Spec.spec Conversion_Spec.spec Deep_Export_Spec.spec From 8f58065234ef4d3667f166705c21220df33f9913 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 29 Jun 2022 17:39:09 +0200 Subject: [PATCH 2/8] Making identity,const,flip,curry,uncurry member functions --- CHANGELOG.md | 2 ++ .../Standard/Base/0.0.0-dev/src/Function.enso | 20 +++++++++---------- test/Tests/src/Data/Function_Spec.enso | 1 + 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b916df667c2..e01119dea06c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -147,6 +147,7 @@ - [Added `File_Format.Delimited` support to `Table.write` for new files.][3528] - [Adjusted `Database.connect` API to new design.][3542] - [Added `File_Format.Excel` support to `Table.write` for new files.][3551] +- [identity,const,flip,curry,uncurry functions][3554] - [Added append support for `File_Format.Excel`.][3558] - [Added support for custom encodings in `File_Format.Delimited` writing.][3564] @@ -235,6 +236,7 @@ [3542]: https://github.com/enso-org/enso/pull/3542 [3551]: https://github.com/enso-org/enso/pull/3551 [3552]: https://github.com/enso-org/enso/pull/3552 +[3554]: https://github.com/enso-org/enso/pull/3554 [3558]: https://github.com/enso-org/enso/pull/3558 [3564]: https://github.com/enso-org/enso/pull/3564 diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso index 0ff13aeb343b..955a7937e373 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso @@ -19,8 +19,8 @@ type Function > Example five = Function.identity 5 # returns number 5 -Function.identity : a -> a -Function.identity x = x +identity : a -> a +identity x = x ## Flips the first two arguments of a function. Returns function that takes two arguments, but in opposite order. @@ -31,8 +31,8 @@ Function.identity x = x > Example Console.log <| Function.flip (+) "world" "hello" # Prints 'helloworld' -Function.flip : (a -> b -> c) -> (b -> a -> c) -Function.flip f = (x -> y -> f y x) +flip : (a -> b -> c) -> (b -> a -> c) +flip f = (x -> y -> f y x) ## Creates a function which drops its input and returns the provided value instead. @@ -44,21 +44,21 @@ Function.flip f = (x -> y -> f y x) > Example Console.log <| [1, 2, 3].map (Function.const 7) # Prints '[7, 7, 7]' -Function.const : a -> b -> a -Function.const x _ = x +const : a -> b -> a +const x _ = x ## Converts a single-argument function accepting a pair of elements into a multi-argument one. Arguments: - f function accepting pair of values -Function.curry : ([a, b] -> c) -> (a -> b -> c) -Function.curry f = x -> y -> f [x, y] +curry : ([a, b] -> c) -> (a -> b -> c) +curry f = x -> y -> f [x, y] ## Converts a multi-argument function into a single-argument one accepting a pair of elements. Arguments: - f function accepting multiple arguments -Function.uncurry : (a -> b -> c) -> ([a, b] -> c) -Function.uncurry f = (pair -> f pair.head pair.tail.head) +uncurry : (a -> b -> c) -> ([a, b] -> c) +uncurry f = (pair -> f pair.head pair.tail.head) diff --git a/test/Tests/src/Data/Function_Spec.enso b/test/Tests/src/Data/Function_Spec.enso index ac2cac4600ba..88e0fc94941f 100644 --- a/test/Tests/src/Data/Function_Spec.enso +++ b/test/Tests/src/Data/Function_Spec.enso @@ -1,5 +1,6 @@ from Standard.Base import all import Standard.Base.Data.Numbers +import Standard.Base.Function import Standard.Test from Standard.Base.Data.Ordering import Equal, Less, Greater From 8fcb36b912c1225efa7ec624fce8ef1f3f53a8f0 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 30 Jun 2022 15:12:12 +0200 Subject: [PATCH 3/8] Avoid importing Function --- test/Tests/src/Data/Function_Spec.enso | 27 +++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/test/Tests/src/Data/Function_Spec.enso b/test/Tests/src/Data/Function_Spec.enso index 88e0fc94941f..089a546de7f9 100644 --- a/test/Tests/src/Data/Function_Spec.enso +++ b/test/Tests/src/Data/Function_Spec.enso @@ -1,43 +1,42 @@ from Standard.Base import all import Standard.Base.Data.Numbers -import Standard.Base.Function import Standard.Test from Standard.Base.Data.Ordering import Equal, Less, Greater spec = - Test.group "Function.identity" <| + Test.group "identity" <| Test.specify "identity on number" <| - (Function.identity 5) . should_equal 5 + (identity 5) . should_equal 5 Test.specify "identity on text" <| - (Function.identity '5') . should_equal '5' + (identity '5') . should_equal '5' Test.specify "identity on boolean" <| - (Function.identity False) . should_equal False + (identity False) . should_equal False - Test.group "Function.flip" <| + Test.group "flip" <| Test.specify "flip on number" <| - (Function.flip (-) 2 5) . should_equal 3 + (flip (-) 2 5) . should_equal 3 Test.specify "flip on text" <| - (Function.flip (+) "world" "hello") . should_equal "helloworld" + (flip (+) "world" "hello") . should_equal "helloworld" - Test.group "Function.const" <| + Test.group "const" <| Test.specify "const on number" <| - two = Function.const 2 + two = const 2 two 5 . should_equal 2 - Test.group "Function.curry" <| + Test.group "curry" <| Test.specify "curry on number list" <| sum = x -> x.fold 0 (+) sum [1, 2, 3, 4] . should_equal 10 - plus = Function.curry sum + plus = curry sum plus 6 3 . should_equal 9 - Test.group "Function.uncurry" <| + Test.group "uncurry" <| Test.specify "uncurry on number list" <| - plus = Function.uncurry (*) + plus = uncurry (*) plus [6, 7] . should_equal 42 From 52f1052488e73d933a238836e007f69f4dca91f5 Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Thu, 7 Jul 2022 16:06:32 +0200 Subject: [PATCH 4/8] reduce import scope, spurious here --- distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso | 2 +- test/Tests/src/Data/Function_Spec.enso | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso index 955a7937e373..12ee9158fcd5 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso @@ -1,4 +1,4 @@ -import Standard.Base +import Standard.Base.Data.Vector # Function types. type Function diff --git a/test/Tests/src/Data/Function_Spec.enso b/test/Tests/src/Data/Function_Spec.enso index 089a546de7f9..7e3abad0dc21 100644 --- a/test/Tests/src/Data/Function_Spec.enso +++ b/test/Tests/src/Data/Function_Spec.enso @@ -40,4 +40,4 @@ spec = plus [6, 7] . should_equal 42 -main = Test.Suite.run_main here.spec +main = Test.Suite.run_main spec From 31a837aea608610733c2ed144b8bcc1c0591c0bb Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 11 Jul 2022 06:44:17 +0200 Subject: [PATCH 5/8] Using pair.second --- distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso index 12ee9158fcd5..76aaef7ba553 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso @@ -61,4 +61,4 @@ curry f = x -> y -> f [x, y] - f function accepting multiple arguments uncurry : (a -> b -> c) -> ([a, b] -> c) -uncurry f = (pair -> f pair.head pair.tail.head) +uncurry f = (pair -> f pair.head pair.second) From 83698270bf15bd17134479487f04e0feba963bd9 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 11 Jul 2022 06:45:25 +0200 Subject: [PATCH 6/8] Function that multiplies shall be called times MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Radosław Waśko --- test/Tests/src/Data/Function_Spec.enso | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/Tests/src/Data/Function_Spec.enso b/test/Tests/src/Data/Function_Spec.enso index 7e3abad0dc21..bb05f43d474d 100644 --- a/test/Tests/src/Data/Function_Spec.enso +++ b/test/Tests/src/Data/Function_Spec.enso @@ -36,8 +36,8 @@ spec = Test.group "uncurry" <| Test.specify "uncurry on number list" <| - plus = uncurry (*) - plus [6, 7] . should_equal 42 + times = uncurry (*) + times [6, 7] . should_equal 42 main = Test.Suite.run_main spec From ac4e796f99ef055550dad7e83b006f82b53e4df1 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 11 Jul 2022 06:47:34 +0200 Subject: [PATCH 7/8] Keep using IO.println --- distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso index 76aaef7ba553..d20c5469f144 100644 --- a/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso +++ b/distribution/lib/Standard/Base/0.0.0-dev/src/Function.enso @@ -29,7 +29,7 @@ identity x = x - f function that takes two arguments > Example - Console.log <| Function.flip (+) "world" "hello" # Prints 'helloworld' + IO.println <| Function.flip (+) "world" "hello" # Prints 'helloworld' flip : (a -> b -> c) -> (b -> a -> c) flip f = (x -> y -> f y x) @@ -42,7 +42,7 @@ flip f = (x -> y -> f y x) - x constant value to return > Example - Console.log <| [1, 2, 3].map (Function.const 7) # Prints '[7, 7, 7]' + IO.println <| [1, 2, 3].map (Function.const 7) # Prints '[7, 7, 7]' const : a -> b -> a const x _ = x From 6bf19d51d48578a63b8800044f4258383a13d290 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Mon, 11 Jul 2022 09:06:10 +0200 Subject: [PATCH 8/8] Process IR.Application.Literal.Sequence when building parameter suggestions --- .../scala/org/enso/compiler/context/SuggestionBuilder.scala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala b/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala index a69e20ef834c..b5d4c7c966a5 100644 --- a/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala +++ b/engine/runtime/src/main/scala/org/enso/compiler/context/SuggestionBuilder.scala @@ -403,6 +403,8 @@ final class SuggestionBuilder[A: IndexedSource](val source: A) { val tdef = resolveTypeName(bindings, typeName) .getOrElse(TypeArg.Value(QualifiedName.simpleName(typeName))) args :+ tdef + case seq: IR.Application.Literal.Sequence => + seq.items.foldLeft(args)((a, t) => go(t, a)) case _ => args }