mirror of
https://github.com/enso-org/enso.git
synced 2024-12-23 10:05:06 +03:00
Update examples for Standard.Base.Data.* (#1707)
This commit is contained in:
parent
7c98ed6014
commit
6060d31c79
10
distribution/std-lib/Standard/data/food_shop_inventory.csv
Normal file
10
distribution/std-lib/Standard/data/food_shop_inventory.csv
Normal file
@ -0,0 +1,10 @@
|
||||
item_name , price , stock
|
||||
Pavlova Cake (Whole) , 10.50 , 10
|
||||
Gruyere (Wheel) , 50 , 3
|
||||
Strawberries (Punnett) , 4.50 , 50
|
||||
Tomatoes (Whole) , 0.5 , 100
|
||||
Tomato and Egg Sandwich , 1.75 , 20
|
||||
Lobster Thermidor , 15.50 , 10
|
||||
Syrniki , 1.99 , 25
|
||||
Bacon and Sausage Sandwich , 1.75 , 15
|
||||
|
|
@ -41,7 +41,7 @@ from Standard.Base.Data.Number.Extensions export all hiding Math, String, Double
|
||||
from Standard.Base.Data.Noise export all hiding Noise
|
||||
from Standard.Base.Data.Pair export Pair
|
||||
from Standard.Base.Data.Range export Range
|
||||
from Standard.Base.Data.Text.Extensions export Text
|
||||
from Standard.Base.Data.Text.Extensions export Text, Split_Kind
|
||||
from Standard.Base.Error.Common export all
|
||||
from Standard.Base.Error.Extensions export all
|
||||
from Standard.Base.Meta.Enso_Project export all
|
||||
|
@ -23,8 +23,13 @@ from Standard.Base import all
|
||||
their own equality operations that will be more efficient than these.
|
||||
|
||||
> Example
|
||||
Checking if 1 is equal to 2.
|
||||
1 == 2
|
||||
Checking if the variable `a` is equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_equality =
|
||||
a = 7 * 21
|
||||
a == 147
|
||||
Any.== : Any -> Boolean
|
||||
Any.== that = if Meta.is_same_object this that then True else
|
||||
this_meta = Meta.meta this
|
||||
@ -57,8 +62,13 @@ Any.== that = if Meta.is_same_object this that then True else
|
||||
satisfy universal equality, as described in the documentation for `Any.==`.
|
||||
|
||||
> Example
|
||||
Checking if 1 is not equal to 2.
|
||||
1 != 2
|
||||
Checking if the variable `a` is not equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_inequality =
|
||||
a = 7 * 21
|
||||
a != 147
|
||||
Any.!= : Any -> Boolean
|
||||
Any.!= that = (this == that).not
|
||||
|
||||
@ -75,8 +85,13 @@ Any.!= that = (this == that).not
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if 1 is greater than 10.
|
||||
1 > 10
|
||||
Checking if the variable `a` is greater than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater =
|
||||
a = 7 * 28
|
||||
a > 147
|
||||
Any.> : Any -> Boolean
|
||||
Any.> that = this.compare_to that == Ordering.Greater
|
||||
|
||||
@ -94,8 +109,13 @@ Any.> that = this.compare_to that == Ordering.Greater
|
||||
greater than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if 1 is greater than or equal to 10.
|
||||
1 >= 10
|
||||
Checking if the variable `a` is greater than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_greater_eq =
|
||||
a = 6 * 21
|
||||
a >= 147
|
||||
Any.>= : Any -> Boolean
|
||||
Any.>= that = (this > that) || (this == that)
|
||||
|
||||
@ -112,8 +132,13 @@ Any.>= that = (this > that) || (this == that)
|
||||
please ensure that it is semantically equivalent to using `.compare_to`.
|
||||
|
||||
> Example
|
||||
Checking if 1 is less than 10.
|
||||
1 < 10
|
||||
Checking if the variable `a` is less than `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
Any.< : Any -> Boolean
|
||||
Any.< that = this.compare_to that == Ordering.Less
|
||||
|
||||
@ -131,8 +156,13 @@ Any.< that = this.compare_to that == Ordering.Less
|
||||
less than than and equal to operations.
|
||||
|
||||
> Example
|
||||
Checking if 1 is less than or equal to 10.
|
||||
1 <= 10
|
||||
Checking if the variable `a` is less than or equal to `147`.
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_less_eq =
|
||||
a = 7 * 21
|
||||
a < 147
|
||||
Any.<= : Any -> Boolean
|
||||
Any.<= that = (this < that) || (this == that)
|
||||
|
||||
@ -143,14 +173,15 @@ Any.<= that = (this < that) || (this == that)
|
||||
|
||||
> Example
|
||||
Checking if the value 1 is nothing.
|
||||
|
||||
1.is_nothing
|
||||
Any.is_nothing : Boolean
|
||||
Any.is_nothing = case this of
|
||||
Nothing -> True
|
||||
_ -> False
|
||||
|
||||
## Executes the provided handler on a dataflow error, or returns a non-error
|
||||
value unchanged.
|
||||
## Executes the provided handler on an error, or returns a non-error value
|
||||
unchanged.
|
||||
|
||||
Arguments:
|
||||
- handler: The function to call on this if it is an error value. By default
|
||||
@ -158,7 +189,12 @@ Any.is_nothing = case this of
|
||||
|
||||
> Example
|
||||
Catching an erroneous value and getting the length of its message.
|
||||
(Time.Time_Error "Message").catch (err -> err.error_message.length)
|
||||
|
||||
from Standard.Base import all
|
||||
|
||||
example_catch =
|
||||
error = Error.throw "My message"
|
||||
error.catch (err -> err.length)
|
||||
Any.catch : (Error -> Any) -> Any
|
||||
Any.catch (handler = x->x) = this.catch_primitive handler
|
||||
|
||||
@ -171,12 +207,24 @@ Any.catch (handler = x->x) = this.catch_primitive handler
|
||||
is an error, the error is transformed using the provided function.
|
||||
|
||||
> Example
|
||||
Wrapping an error value.
|
||||
map.get "x" . map_error (_ -> ElementNotFound "x")
|
||||
Transforming an error value to provide more information.
|
||||
|
||||
from Standard.Base import all
|
||||
from Standard.Examples import Example_Error_Type
|
||||
|
||||
example_map_error =
|
||||
my_map = Map.empty
|
||||
error = my_map.get "x"
|
||||
error.map_error (_ -> Example_Error_Type "x is missing")
|
||||
Any.map_error : (Error -> Error) -> Any
|
||||
Any.map_error _ = this
|
||||
|
||||
## Checks if `this` is an error.
|
||||
|
||||
> Example
|
||||
Checking if the provided value is an error.
|
||||
|
||||
1.is_error
|
||||
Any.is_error : Boolean
|
||||
Any.is_error = False
|
||||
|
||||
@ -192,6 +240,7 @@ Any.is_error = False
|
||||
|
||||
> Example
|
||||
Applying a function to a block.
|
||||
|
||||
(x -> x + 1) <|
|
||||
y = 1 ^ 3
|
||||
3 + y
|
||||
@ -214,6 +263,7 @@ Any.<| ~argument = this argument
|
||||
> Example
|
||||
Applying multiple functions in a pipeline to compute a number and transform
|
||||
it to text.
|
||||
|
||||
1 |> (* 2) |> (/ 100) |> .to_text
|
||||
Any.|> : (Any -> Any) -> Any
|
||||
Any.|> ~function = function this
|
||||
@ -226,6 +276,7 @@ Any.|> ~function = function this
|
||||
|
||||
> Example
|
||||
Multiply by 2 and then add 1 as a function applied to 2.
|
||||
|
||||
(+1 << *2) 2
|
||||
Any.<< : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
Any.<< ~that = x -> this (that x)
|
||||
@ -238,6 +289,7 @@ Any.<< ~that = x -> this (that x)
|
||||
|
||||
> Example
|
||||
Add one and then multiply by two as a function applied to 2.
|
||||
|
||||
(+1 >> *2) 2
|
||||
Any.>> : (Any -> Any) -> (Any -> Any) -> Any -> Any
|
||||
Any.>> ~that = x -> that (this x)
|
||||
@ -256,6 +308,7 @@ Any.>> ~that = x -> that (this x)
|
||||
|
||||
> Example
|
||||
Converting the number `2` into visualization data.
|
||||
|
||||
2.to_default_visualization_data
|
||||
Any.to_default_visualization_data : Text
|
||||
Any.to_default_visualization_data = this.to_json.to_text
|
||||
|
@ -11,6 +11,7 @@ from Standard.Base import all
|
||||
|
||||
> Example
|
||||
Converting an array to its default visualization representation.
|
||||
|
||||
[1, 2, 3, 4].to_array.to_default_visualization_data
|
||||
Array.to_default_visualization_data : Text
|
||||
Array.to_default_visualization_data =
|
||||
|
@ -8,7 +8,10 @@ export Standard.Base.Data.Interval.Bound
|
||||
|
||||
> Example
|
||||
Create the bounds-exclusive range from 1 to 5.
|
||||
Interval.exclusive 1 5
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_exclusive = Interval.exclusive 1 5
|
||||
exclusive : Any -> Any -> Interval
|
||||
exclusive start end = Interval (Bound.Exclusive start) (Bound.Exclusive end)
|
||||
|
||||
@ -16,7 +19,10 @@ exclusive start end = Interval (Bound.Exclusive start) (Bound.Exclusive end)
|
||||
|
||||
> Example
|
||||
Create the start-exclusive range from 1 to 5.
|
||||
Interval.start_exclusive 1 5
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_start_exclusive = Interval.start_exclusive 1 5
|
||||
start_exclusive : Any -> Any -> Interval
|
||||
start_exclusive start end = Interval (Bound.Exclusive start) (Bound.Inclusive end)
|
||||
|
||||
@ -24,22 +30,30 @@ start_exclusive start end = Interval (Bound.Exclusive start) (Bound.Inclusive en
|
||||
|
||||
> Example
|
||||
Create the end-exclusive range from 1 to 5.
|
||||
Interval.end_exclusive 1 5
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_end_exclusive = Interval.end_exclusive 1 5
|
||||
end_exclusive : Any -> Any -> Interval
|
||||
end_exclusive start end = Interval (Bound.Inclusive start) (Bound.Exclusive end)
|
||||
|
||||
## Creates an interval that includes its upper bound.
|
||||
## Creates an interval that includes both of its bounds.
|
||||
|
||||
> Example
|
||||
Create the inclusive range from 1 to 5.
|
||||
Interval.inclusive 1 5
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_inclusive = Interval.inclusive 1 5
|
||||
inclusive : Any -> Any -> Interval
|
||||
inclusive start end = Interval (Bound.Inclusive start) (Bound.Inclusive end)
|
||||
|
||||
## An interval type
|
||||
type Interval
|
||||
|
||||
## A type representing an interval over orderable types.
|
||||
## PRIVATE
|
||||
|
||||
A type representing an interval over orderable types.
|
||||
|
||||
Arguments:
|
||||
- start: The start of the interval.
|
||||
@ -53,7 +67,10 @@ type Interval
|
||||
|
||||
> Example
|
||||
Checking if the interval 1 to 5 contains 7.
|
||||
(Interval.inclusive 1 5) . contains 7
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_contains = (Interval.inclusive 1 5) . contains 7
|
||||
contains : Any -> Boolean
|
||||
contains that = if this.start.n > this.end.n then False else
|
||||
case this.start of
|
||||
@ -68,7 +85,10 @@ type Interval
|
||||
|
||||
> Example
|
||||
Check if the interval from 0 to 0 is empty.
|
||||
Interval.inclusive 0 0 . is_empty
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_is_empty = Interval.inclusive 0 0 . is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = case this.start of
|
||||
Bound.Exclusive s -> case this.end of
|
||||
@ -82,7 +102,10 @@ type Interval
|
||||
|
||||
> Example
|
||||
Check if the interval from 0 to 1 is not empty.
|
||||
Interval.inclusive 0 1 . not_empty
|
||||
|
||||
import Standard.Base.Data.Interval
|
||||
|
||||
example_not_empty = Interval.inclusive 0 1 . not_empty
|
||||
not_empty : Boolean
|
||||
not_empty = this.is_empty.not
|
||||
|
||||
|
@ -12,7 +12,10 @@ type Bound
|
||||
|
||||
> Example
|
||||
Create a bound that includes the value 2.
|
||||
Inclusive 2
|
||||
|
||||
import Standard.Base.Data.Interval.Bound
|
||||
|
||||
example_bound_inclusive = Bound.Inclusive 2
|
||||
type Inclusive n
|
||||
|
||||
## A bound that excludes the value `n`.
|
||||
@ -22,5 +25,8 @@ type Bound
|
||||
|
||||
> Example
|
||||
Create a bound that excludes the value 2.
|
||||
Exclusive 2
|
||||
|
||||
import Standard.Base.Data.Interval.Bound
|
||||
|
||||
example_bound_exclusive = Bound.Exclusive 2.
|
||||
type Exclusive n
|
||||
|
@ -2,6 +2,47 @@ from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Json.Internal
|
||||
|
||||
## A smart constructor, building an object representation based on a vector
|
||||
of key-value pairs.
|
||||
|
||||
Arguments:
|
||||
- contents: A vector of key-value pairs.
|
||||
|
||||
All values used as keys must define a `to_json_key : Text` method.
|
||||
|
||||
> Example
|
||||
The following code returns a JSON object, that after serialization becomes
|
||||
{ "foo": 533, "bar": false }
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
|
||||
example_from_pairs = Json.from_pairs [["foo", 533], ["bar", False]]
|
||||
from_pairs : Vector.Vector -> Object
|
||||
from_pairs contents =
|
||||
fs = contents.fold Map.empty map-> kv_pair->
|
||||
key = kv_pair . at 0 . to_json_key
|
||||
val = kv_pair . at 1 . to_json
|
||||
map.insert key val
|
||||
Object fs
|
||||
|
||||
## Parses an RFC-8259 compliant JSON text into a `Json` structure.
|
||||
|
||||
Arguments:
|
||||
- json_text: The RFC-8259-compliant JSON text.
|
||||
|
||||
> Example
|
||||
Convert some text representing a JSON object into JSON.
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
|
||||
example_parse = Json.parse '{ "a": 1 }'
|
||||
parse : Text -> Json ! Parse_Error
|
||||
parse json_text =
|
||||
r = Panic.recover (Internal.parse_helper json_text)
|
||||
r.catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Parse_Error err.getMessage)
|
||||
p -> Panic.throw p
|
||||
|
||||
## Represents a JSON structure.
|
||||
type Json
|
||||
|
||||
@ -50,51 +91,37 @@ type Json
|
||||
The following shows an example of reading a nested JSON into a desired
|
||||
type. It will return a vector of `Book` objects containing data from
|
||||
`json_string`.
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
import Standard.Examples
|
||||
|
||||
type Book title author
|
||||
type Author name year_of_birth
|
||||
|
||||
read_data =
|
||||
json_string = '''
|
||||
[
|
||||
{
|
||||
"title": "Lord of the Rings",
|
||||
"author": {
|
||||
"name": "J. R. R. Tolkien",
|
||||
"year_of_birth": 1892
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "The Little Prince",
|
||||
"author": {
|
||||
"name": "Antoine de Saint-Exupéry",
|
||||
"year_of_birth": 1900
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "And Then There Were None",
|
||||
"author": {
|
||||
"name": "Agatha Christie",
|
||||
"year_of_birth": 1890
|
||||
}
|
||||
}
|
||||
]
|
||||
parsed = Json.parse json_string
|
||||
parsed.into (Vector (Book title=Text (Author name=Text year_of_birth=Number)))
|
||||
example_into =
|
||||
json = Examples.json
|
||||
format = (Vector.Vector (Book title=Text (Author name=Text year_of_birth=Number)))
|
||||
json.into format
|
||||
into : Any -> Any ! Marshalling_Error
|
||||
into type_descriptor =
|
||||
Panic.recover (Internal.into_helper type_descriptor this)
|
||||
|
||||
## Returns this Json object.
|
||||
|
||||
Included to implement the `to_json` interface.
|
||||
This is a no-op on a JSON object, but is included to implement the
|
||||
`to_json` interface.
|
||||
to_json : Json
|
||||
to_json = this
|
||||
|
||||
## Renders this JSON into an RFC-8259 compliant text.
|
||||
|
||||
> Example
|
||||
Convert a JSON number to text.
|
||||
Json.Number 3 . to_text
|
||||
Convert some JSON to text.
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
import Standard.Examples
|
||||
|
||||
example_to_text = Examples.json.to_text
|
||||
to_text : Text
|
||||
to_text = Internal.render_helper "" this
|
||||
|
||||
@ -102,7 +129,10 @@ type Json
|
||||
|
||||
> Example
|
||||
Unwrap the JSON number 3 to the primitive number 3.
|
||||
Json.Number 3 . unwrap
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
|
||||
example_unwrap = Json.Number 3 . unwrap
|
||||
unwrap : Any
|
||||
unwrap = case this of
|
||||
Json.Array its -> its.map .unwrap
|
||||
@ -131,23 +161,16 @@ Parse_Error.to_display_text = "Parse error in parsing JSON: " + this.message.to_
|
||||
- field: The name of the field from which to get the value.
|
||||
|
||||
Throws `Nothing` if the associated key is not defined.
|
||||
Object.get : Text -> Json ! Nothing
|
||||
Object.get field = this.fields.get field
|
||||
|
||||
## Parses an RFC-8259 compliant JSON text into a `Json` structure.
|
||||
|
||||
Arguments:
|
||||
- json_text: The RFC-8259-compliant JSON text.
|
||||
|
||||
> Example
|
||||
Convert some text representing a JSON object into JSON.
|
||||
"{ "a": 1 }".parse
|
||||
parse : Text -> Json ! Parse_Error
|
||||
parse json_text =
|
||||
r = Panic.recover (Internal.parse_helper json_text)
|
||||
r.catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Parse_Error err.getMessage)
|
||||
p -> Panic.throw p
|
||||
Get the "title" field from this JSON representing a book.
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
import Standard.Examples
|
||||
|
||||
example_get = Examples.json_object.get "title"
|
||||
Object.get : Text -> Json ! Nothing
|
||||
Object.get field = this.fields.get field
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
@ -215,6 +238,7 @@ Any.to_json =
|
||||
Object with_tp
|
||||
Meta.Constructor _ ->
|
||||
Object (Map.empty . insert "type" (String m.name))
|
||||
|
||||
## The following two cases cannot be handled generically and should
|
||||
instead define their own `to_json` implementations.
|
||||
Meta.Polyglot _ -> Null
|
||||
@ -243,24 +267,3 @@ Base.Boolean.to_json = Boolean this
|
||||
Nothing.to_json
|
||||
Nothing.to_json : Null
|
||||
Nothing.to_json = Null
|
||||
|
||||
## A smart constructor, building an object representation based on a vector
|
||||
of key-value pairs.
|
||||
|
||||
Arguments:
|
||||
- contents: A vector of key-value pairs.
|
||||
|
||||
All values used as keys must define a `to_json_key : Text` method.
|
||||
|
||||
> Example
|
||||
The following code:
|
||||
Json.from_pairs [["foo", 533], ["bar", False]]
|
||||
Returns a JSON object, that after serialization becomes:
|
||||
{ "foo": 533, "bar": false }
|
||||
from_pairs : Vector.Vector -> Object
|
||||
from_pairs contents =
|
||||
fs = contents.fold Map.empty map-> kv_pair->
|
||||
key = kv_pair . at 0 . to_json_key
|
||||
val = kv_pair . at 1 . to_json
|
||||
map.insert key val
|
||||
Object fs
|
||||
|
@ -1,24 +1,6 @@
|
||||
from Builtins import all
|
||||
from Builtins export Nil, Cons
|
||||
|
||||
## PRIVATE
|
||||
A helper for the `map` function.
|
||||
|
||||
Arguments:
|
||||
- list: The list to map over.
|
||||
- cons: The current field to set.
|
||||
- f: The function to apply to the value.
|
||||
|
||||
Uses unsafe field mutation under the hood, to rewrite `map` in
|
||||
a tail-recursive manner. The mutation is purely internal and does not leak
|
||||
to the user-facing API.
|
||||
map_helper list cons f = case list of
|
||||
Cons h t ->
|
||||
res = Cons (f h) Nil
|
||||
Unsafe.set_atom_field cons 1 res
|
||||
@Tail_Call here.map_helper t res f
|
||||
Nil -> Unsafe.set_atom_field cons 1 Nil
|
||||
|
||||
## The basic cons-list type.
|
||||
|
||||
A cons-list allows storage of an arbitrary number of elements.
|
||||
@ -37,7 +19,10 @@ type List
|
||||
|
||||
> Example
|
||||
Get the length of a two item list.
|
||||
(Cons 1 (Cons 2 Nil)).length
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_length = Examples.list.length
|
||||
length : Number
|
||||
length = this.fold 0 (acc -> _ -> acc + 1)
|
||||
|
||||
@ -55,8 +40,11 @@ type List
|
||||
|
||||
> Example
|
||||
In the following example, we'll compute the sum of all elements of a
|
||||
list:
|
||||
(Cons 0 <| Cons 1 <| Cons 2 <| Nil) . fold 0 (+)
|
||||
list.
|
||||
|
||||
import Standard.Eamples
|
||||
|
||||
example_fold = Examples.list.fold 0 (+)
|
||||
fold : Any -> (Any -> Any -> Any) -> Any
|
||||
fold init f =
|
||||
go acc list = case list of
|
||||
@ -73,9 +61,11 @@ type List
|
||||
function.
|
||||
|
||||
> Example
|
||||
In the following example, we'll check if any element of the list is
|
||||
larger than `5`:
|
||||
(Cons 0 <| Cons 1 <| Cons 2 <| Nil) . exists (> 5)
|
||||
Check if any element of the list is larger than 5.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_exists = Examples.list.exists (> 5)
|
||||
exists : (Any -> Boolean) -> Boolean
|
||||
exists predicate =
|
||||
go list = case list of
|
||||
@ -96,9 +86,11 @@ type List
|
||||
a Boolean value.
|
||||
|
||||
> Example
|
||||
In the following example, we'll check if any element of the list is
|
||||
larger than `5`:
|
||||
(Cons 0 <| Cons 1 <| Cons 2 <| Nil) . any (> 5)
|
||||
Check if any element of the list is larger than 5.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_any = Examples.list.any (> 5)
|
||||
any : (Any -> Boolean) -> Boolean
|
||||
any predicate = this.exists predicate
|
||||
|
||||
@ -110,8 +102,11 @@ type List
|
||||
function.
|
||||
|
||||
> Example
|
||||
Check if all elements in the list are less than zero.
|
||||
(Cons 1 (Cons 2 Nil)).all (< 0)
|
||||
Check if all elements in the list are greater than zero.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_all = Examples.list.all (> 0)
|
||||
all : (Any -> Boolean) -> Boolean
|
||||
all predicate = this.fold True (l -> r -> l && predicate r)
|
||||
|
||||
@ -121,8 +116,11 @@ type List
|
||||
- elem: The element to check if it is in the list.
|
||||
|
||||
> Example
|
||||
Checking if the list contains the number 72.
|
||||
(Cons 1 (Cons 72 Nil)).contains 72
|
||||
Checking if the list contains the number 3.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_contains = Examples.list.contains 3
|
||||
contains : Any -> Boolean
|
||||
contains elem = this.exists ix-> ix == elem
|
||||
|
||||
@ -130,8 +128,10 @@ type List
|
||||
|
||||
> Example
|
||||
Checking for emptiness.
|
||||
Nil.is_empty == true
|
||||
Cons 1 Nil . is_empty == false
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_empty = Examples.list.is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = this.length == 0
|
||||
|
||||
@ -139,8 +139,10 @@ type List
|
||||
|
||||
> Example
|
||||
Checking for emptiness.
|
||||
Nil.not_empty == false
|
||||
Cons 1 Nil . not_empty == true
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_not_empty = Examples.list.not_empty
|
||||
not_empty : Boolean
|
||||
not_empty = this.is_empty.not
|
||||
|
||||
@ -152,8 +154,11 @@ type List
|
||||
function.
|
||||
|
||||
> Example
|
||||
Selecting all elements that are greater than 3.
|
||||
(Cons 1 Nil).filter (> 3)
|
||||
Selecting all elements that are less than 3.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_filter = Examples.list.filter (< 3)
|
||||
filter : (Any -> Boolean) -> List
|
||||
filter predicate =
|
||||
case this of
|
||||
@ -170,7 +175,10 @@ type List
|
||||
|
||||
> Example
|
||||
Add `1` to each element of the list:
|
||||
(Cons 0 <| Cons 1 <| Cons 2 <| Nil) . map +1
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_map = Examples.list.map +1
|
||||
map : (Any -> Any) -> List
|
||||
map f = case this of
|
||||
Nil -> Nil
|
||||
@ -189,7 +197,10 @@ type List
|
||||
|
||||
> Example
|
||||
Print each of the list elements to the standard output.
|
||||
(Cons 0 <| Cons 1 <| Cons 2 <| Nil) . each IO.println
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_each = Examples.list.each IO.println
|
||||
each : (Any -> Any) -> Nothing
|
||||
each f =
|
||||
go list = case list of
|
||||
@ -205,7 +216,10 @@ type List
|
||||
|
||||
> Example
|
||||
Reversing a small list.
|
||||
(Cons 1 (Cons 2 Nil)).reverse == (Cons 2 (Cons 1 Nil))
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_reverse = Examples.list.reverse
|
||||
reverse : List
|
||||
reverse = this.fold Nil (l -> el -> Cons el l)
|
||||
|
||||
@ -217,7 +231,10 @@ type List
|
||||
|
||||
> Example
|
||||
Removing the first element from a list.
|
||||
(Cons 1 (Cons 2 (Nil))).drop_start 1
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_drop_start = Examples.list.drop_start 1
|
||||
drop_start : Integer -> List
|
||||
drop_start count = if count <= 0 then this else case this of
|
||||
Cons _ b -> b.drop_start count-1
|
||||
@ -231,7 +248,10 @@ type List
|
||||
|
||||
> Example
|
||||
Obtaining the first 2 elements of a list.
|
||||
(Cons 1 (Cons 2 (Cons 3 Nil))).take_start 2
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_take_start = Examples.list.take_start 2
|
||||
take_start : Integer -> List
|
||||
take_start count = if count <= 0 then Nil else case this of
|
||||
Cons a b -> Cons a (b.take_start count-1)
|
||||
@ -241,7 +261,10 @@ type List
|
||||
|
||||
> Example
|
||||
This returns 1.
|
||||
(Cons 1 (Cons 2 Nil)).head
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_head = Examples.list.head
|
||||
head : Any ! Nothing
|
||||
head = case this of
|
||||
Cons a _ -> a
|
||||
@ -251,7 +274,10 @@ type List
|
||||
|
||||
> Example
|
||||
This returns (Cons 2 Nil).
|
||||
(Cons 1 (Cons 2 Nil)).tail
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_tail = Examples.list.tail
|
||||
tail : List ! Nothing
|
||||
tail = case this of
|
||||
Cons _ b -> b
|
||||
@ -261,7 +287,10 @@ type List
|
||||
|
||||
> Example
|
||||
Removing the last element of the list to give (Cons 1 Nil).
|
||||
(Cons 1 (Cons 2 Nil)).init
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_init = Examples.list.init
|
||||
init : List ! Nothing
|
||||
init =
|
||||
init' x y = case y of
|
||||
@ -274,8 +303,11 @@ type List
|
||||
## Get the last element of the list.
|
||||
|
||||
> Example
|
||||
Getting the final element, in this case 2.
|
||||
(Cons 1 (Cons 2 Nil)).last
|
||||
Getting the final element of the list.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_last = Examples.list.last
|
||||
last : Any | Nothing
|
||||
last = case this.fold Nothing (_ -> r -> r) of
|
||||
Nothing -> Error.throw Nothing
|
||||
@ -284,15 +316,40 @@ type List
|
||||
## Get the first element from the list.
|
||||
|
||||
> Example
|
||||
This returns 1.
|
||||
(Cons 1 (Cons 2 Nil)).first
|
||||
Getting the first element in the list.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_first = Examples.list.first
|
||||
first : Any ! Nothing
|
||||
first = this.head
|
||||
|
||||
## Get all elements from the list except the first.
|
||||
|
||||
> Example
|
||||
This returns (Cons 2 Nil).
|
||||
(Cons 1 (Cons 2 Nil)).rest
|
||||
Getting all elements in the list except the first.
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_rest = Examples.list.rest
|
||||
rest : List ! Nothing
|
||||
rest = this.tail
|
||||
|
||||
## PRIVATE
|
||||
A helper for the `map` function.
|
||||
|
||||
Arguments:
|
||||
- list: The list to map over.
|
||||
- cons: The current field to set.
|
||||
- f: The function to apply to the value.
|
||||
|
||||
Uses unsafe field mutation under the hood, to rewrite `map` in
|
||||
a tail-recursive manner. The mutation is purely internal and does not leak
|
||||
to the user-facing API.
|
||||
map_helper list cons f = case list of
|
||||
Cons h t ->
|
||||
res = Cons (f h) Nil
|
||||
Unsafe.set_atom_field cons 1 res
|
||||
@Tail_Call here.map_helper t res f
|
||||
Nil -> Unsafe.set_atom_field cons 1 Nil
|
||||
|
||||
|
@ -18,7 +18,10 @@ default = here.from_java JavaLocale.ROOT
|
||||
|
||||
> Example
|
||||
Get the Bangladeshi locale.
|
||||
Locale.bangladesh
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.bangladesh
|
||||
bangladesh : Locale
|
||||
bangladesh = here.from_language_tag "bn-BD"
|
||||
|
||||
@ -26,7 +29,10 @@ bangladesh = here.from_language_tag "bn-BD"
|
||||
|
||||
> Example
|
||||
Get the Brazilian locale.
|
||||
Locale.brazil
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.brazil
|
||||
brazil : Locale
|
||||
brazil = here.from_language_tag "pt-BR"
|
||||
|
||||
@ -34,7 +40,10 @@ brazil = here.from_language_tag "pt-BR"
|
||||
|
||||
> Example
|
||||
Get the Canadian english locale.
|
||||
Locale.canada_english
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.canada_english
|
||||
canada_english : Locale
|
||||
canada_english = here.from_language_tag "en-CA"
|
||||
|
||||
@ -42,7 +51,10 @@ canada_english = here.from_language_tag "en-CA"
|
||||
|
||||
> Example
|
||||
Get the Canadian french locale.
|
||||
Locale.canada_french
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.canada_french
|
||||
canada_french : Locale
|
||||
canada_french = here.from_language_tag "fr-CA"
|
||||
|
||||
@ -50,7 +62,10 @@ canada_french = here.from_language_tag "fr-CA"
|
||||
|
||||
> Example
|
||||
Get the PRC locale.
|
||||
Locale.china
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.china
|
||||
china : Locale
|
||||
china = here.from_language_tag "zh-CN"
|
||||
|
||||
@ -58,7 +73,10 @@ china = here.from_language_tag "zh-CN"
|
||||
|
||||
> Example
|
||||
Get the French locale.
|
||||
Locale.france
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.france
|
||||
france : Locale
|
||||
france = here.from_language_tag "fr-FR"
|
||||
|
||||
@ -66,7 +84,10 @@ france = here.from_language_tag "fr-FR"
|
||||
|
||||
> Example
|
||||
Get the German locale.
|
||||
Locale.germany
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.germany
|
||||
germany : Locale
|
||||
germany = here.from_language_tag "de-DE"
|
||||
|
||||
@ -74,7 +95,10 @@ germany = here.from_language_tag "de-DE"
|
||||
|
||||
> Example
|
||||
Get the Indian hindi locale.
|
||||
Locale.india_hindi
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.india_hindi
|
||||
india_hindi : Locale
|
||||
india_hindi = here.from_language_tag "hi-IN"
|
||||
|
||||
@ -82,7 +106,10 @@ india_hindi = here.from_language_tag "hi-IN"
|
||||
|
||||
> Example
|
||||
Get the Indian english locale.
|
||||
Locale.indian_english
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.india_english
|
||||
india_english : Locale
|
||||
india_english = here.from_language_tag "en-IN"
|
||||
|
||||
@ -90,7 +117,10 @@ india_english = here.from_language_tag "en-IN"
|
||||
|
||||
> Example
|
||||
Get the Indonesian locale.
|
||||
Locale.indonesia
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.indonesia
|
||||
indonesia : Locale
|
||||
indonesia = here.from_language_tag "id-ID"
|
||||
|
||||
@ -98,7 +128,10 @@ indonesia = here.from_language_tag "id-ID"
|
||||
|
||||
> Example
|
||||
Get the Italian locale.
|
||||
Locale.italy
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.italy
|
||||
italy : Locale
|
||||
italy = here.from_language_tag "it-IT"
|
||||
|
||||
@ -106,7 +139,10 @@ italy = here.from_language_tag "it-IT"
|
||||
|
||||
> Example
|
||||
Get the Japanese locale.
|
||||
Locale.japan
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.japan
|
||||
japan : Locale
|
||||
japan = here.from_language_tag "jp-JP"
|
||||
|
||||
@ -114,7 +150,10 @@ japan = here.from_language_tag "jp-JP"
|
||||
|
||||
> Example
|
||||
Get the Mexican locale.
|
||||
Locale.mexico
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.mexico
|
||||
mexico : Locale
|
||||
mexico = here.from_language_tag "es-MX"
|
||||
|
||||
@ -122,7 +161,10 @@ mexico = here.from_language_tag "es-MX"
|
||||
|
||||
> Example
|
||||
Get the Nigerian locale.
|
||||
Locale.nigeria
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.nigeria
|
||||
nigeria : Locale
|
||||
nigeria = here.from_language_tag "en-NG"
|
||||
|
||||
@ -130,7 +172,10 @@ nigeria = here.from_language_tag "en-NG"
|
||||
|
||||
> Example
|
||||
Get the Pakistani urdu locale.
|
||||
Locale.pakistan_urdu
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.pakistan_urdu
|
||||
pakistan_urdu : Locale
|
||||
pakistan_urdu = here.from_language_tag "ur-PK"
|
||||
|
||||
@ -138,7 +183,10 @@ pakistan_urdu = here.from_language_tag "ur-PK"
|
||||
|
||||
> Example
|
||||
Get the Pakistani english locale.
|
||||
Locale.bangladesh
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.pakistan_english
|
||||
pakistan_english : Locale
|
||||
pakistan_english = here.from_language_tag "en-PK"
|
||||
|
||||
@ -146,7 +194,10 @@ pakistan_english = here.from_language_tag "en-PK"
|
||||
|
||||
> Example
|
||||
Get the Russian locale.
|
||||
Locale.russia
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.russia
|
||||
russia : Locale
|
||||
russia = here.from_language_tag "ru-RU"
|
||||
|
||||
@ -154,7 +205,10 @@ russia = here.from_language_tag "ru-RU"
|
||||
|
||||
> Example
|
||||
Get the South Korean locale.
|
||||
Locale.south_korea
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.south_korea
|
||||
south_korea : Locale
|
||||
south_korea = here.from_language_tag "ko-KR"
|
||||
|
||||
@ -162,7 +216,10 @@ south_korea = here.from_language_tag "ko-KR"
|
||||
|
||||
> Example
|
||||
Get the british locale.
|
||||
Locale.uk
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.uk
|
||||
uk : Locale
|
||||
uk = here.from_language_tag "en-GB"
|
||||
|
||||
@ -170,7 +227,10 @@ uk = here.from_language_tag "en-GB"
|
||||
|
||||
> Example
|
||||
Get the US locale.
|
||||
Locale.us
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_locale = Locale.us
|
||||
us : Locale
|
||||
us = here.from_language_tag "en-US"
|
||||
|
||||
@ -183,7 +243,10 @@ us = here.from_language_tag "en-US"
|
||||
|
||||
> Example
|
||||
A locale representing en-GB.UTF-8.
|
||||
Locale.new "en" "GB" "UTF-8"
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_new = Locale.new "en" "GB" "UTF-8"
|
||||
new : Text -> Text | Nothing -> Text | Nothing -> Locale
|
||||
new language country=Nothing variant=Nothing =
|
||||
country_text = if country.is_nothing then "" else country
|
||||
@ -218,7 +281,10 @@ new language country=Nothing variant=Nothing =
|
||||
|
||||
> Example
|
||||
Creating the locale en_US.
|
||||
Locale.from_language_tag "en_US"
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_from_tag = Locale.from_language_tag "en_US"
|
||||
from_language_tag : Text -> Locale
|
||||
from_language_tag tag =
|
||||
java_locale = JavaLocale.forLanguageTag tag
|
||||
@ -232,7 +298,8 @@ from_language_tag tag =
|
||||
- A variant, which is optional.
|
||||
type Locale
|
||||
|
||||
## A type representing a locale.
|
||||
## PRIVATE
|
||||
A type representing a locale.
|
||||
|
||||
Arguments:
|
||||
- java_locale: The Java locale representation used internally.
|
||||
@ -242,7 +309,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the language tag from the default locale.
|
||||
Locale.default.language
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_language = Locale.default.language
|
||||
language : Text | Nothing
|
||||
language =
|
||||
lang = this.java_locale.getLanguage
|
||||
@ -252,7 +322,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the country tag from the default locale.
|
||||
Locale.default.country
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_country = Locale.default.country
|
||||
country : Text | Nothing
|
||||
country =
|
||||
place = this.java_locale.getCountry
|
||||
@ -262,7 +335,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the variant tag from the default locale.
|
||||
Locale.default.variant
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_variant = Locale.default.variant
|
||||
variant : Text | Nothing
|
||||
variant =
|
||||
var = this.java_locale.getVariant
|
||||
@ -273,7 +349,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the display language tag from the default locale.
|
||||
Locale.default.display_language
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_display_language = Locale.default.display_language
|
||||
display_language : Text | Nothing
|
||||
display_language =
|
||||
disp = this.java_locale.getDisplayLanguage
|
||||
@ -284,7 +363,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the display country tag from the default locale.
|
||||
Locale.default.display_country
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_display_country = Locale.default.display_country
|
||||
display_country : Text | Nothing
|
||||
display_country =
|
||||
disp = this.java_locale.getDisplayCountry
|
||||
@ -295,7 +377,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Get the display variant tag from the default locale.
|
||||
Locale.default.display_variant
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_display_variant = Locale.default.display_variant
|
||||
display_variant : Text | Nothing
|
||||
display_variant =
|
||||
disp = this.java_locale.getDisplayVariant
|
||||
@ -305,7 +390,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Convert the default locale to text.
|
||||
Locale.default.to_text
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_to_text = Locale.default.to_text
|
||||
to_text : Text | Nothing
|
||||
to_text = this.java_locale.toLanguageTag
|
||||
|
||||
@ -313,7 +401,10 @@ type Locale
|
||||
|
||||
> Example
|
||||
Convert the default locale to JSON.
|
||||
Locale.default.to_json
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_to_json = Locale.default.to_json
|
||||
to_json : Json.Object
|
||||
to_json =
|
||||
b = Vector.new_builder
|
||||
@ -334,3 +425,4 @@ from_java java = Locale java
|
||||
|
||||
## PRIVATE
|
||||
javaLocaleBuilder = JavaLocale.Builder
|
||||
|
||||
|
@ -2,19 +2,14 @@ from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Map.Internal
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
An error for getting a missing value from a map.
|
||||
|
||||
Arguments:
|
||||
- key: The key that was asked for.
|
||||
type No_Value_For_Key key
|
||||
|
||||
## Returns an empty map.
|
||||
|
||||
> Example
|
||||
Create an empty map.
|
||||
Map.empty
|
||||
|
||||
import Standard.Base.Data.Map.Internal
|
||||
|
||||
example_empty = Map.empty
|
||||
empty : Map
|
||||
empty = Tip
|
||||
|
||||
@ -26,7 +21,10 @@ empty = Tip
|
||||
|
||||
> Example
|
||||
Create a single element map storing the key 1 and the value 2.
|
||||
Map.singleton 1 2
|
||||
|
||||
import Standard.Base.Data.Map.Internal
|
||||
|
||||
example_singleton = Map.singleton 1 2
|
||||
singleton : Any -> Any -> Map
|
||||
singleton key value = Bin 1 key value Tip Tip
|
||||
|
||||
@ -37,7 +35,10 @@ singleton key value = Bin 1 key value Tip Tip
|
||||
|
||||
> Example
|
||||
Building a map containing two key-value pairs.
|
||||
Map.from_vector [[1, 2], [3, 4]]
|
||||
|
||||
import Standard.Base.Data.Map.Internal
|
||||
|
||||
example_from_vector = Map.from_vector [[1, 2], [3, 4]]
|
||||
from_vector : Vector.Vector -> Map
|
||||
from_vector vec = vec.fold Map.empty (m -> el -> m.insert (el.at 0) (el.at 1))
|
||||
|
||||
@ -65,8 +66,12 @@ type Map
|
||||
## Checks if the map is empty.
|
||||
|
||||
> Example
|
||||
Check if a singleton map is empty.
|
||||
Map.singleton 1 2 . is_empty
|
||||
Check if a map is empty.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_is_empty = Examples.map.is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = case this of
|
||||
Bin _ _ _ _ _ -> False
|
||||
@ -75,16 +80,24 @@ type Map
|
||||
## Checks if the map is not empty.
|
||||
|
||||
> Example
|
||||
Check if a singleton map is not empty.
|
||||
Map.singleton 1 2 . not_empty
|
||||
Check if a map is not empty.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_not_empty = Examples.map.not_empty
|
||||
not_empty : Boolean
|
||||
not_empty = this.is_empty.not
|
||||
|
||||
## Returns the number of entries in this map.
|
||||
|
||||
> Example
|
||||
Get the size of a singleton map.
|
||||
Map.singleton 1 2 . size
|
||||
Get the size of a map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_size = Examples.map.size
|
||||
size : Integer
|
||||
size = case this of
|
||||
Bin s _ _ _ _ -> s
|
||||
@ -95,8 +108,12 @@ type Map
|
||||
The returned vector is sorted in the increasing order of keys.
|
||||
|
||||
> Example
|
||||
Convert a singleton map to a vector.
|
||||
Map.singleton 1 2 . to_vector
|
||||
Convert a map to a vector.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_to_vector = Examples.map.to_vector
|
||||
to_vector : Vector.Vector
|
||||
to_vector =
|
||||
builder = Vector.new_builder
|
||||
@ -114,8 +131,12 @@ type Map
|
||||
## Returns a text representation of this map.
|
||||
|
||||
> Example
|
||||
Convert an empty map to text.
|
||||
Map.empty.to_text
|
||||
Convert a map to text.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_to_text = Examples.map.to_text
|
||||
to_text : Text
|
||||
to_text = this.to_vector.to_text
|
||||
|
||||
@ -128,8 +149,14 @@ type Map
|
||||
associated with each key are pairwise equal.
|
||||
|
||||
> Example
|
||||
Checking two singleton maps for equality.
|
||||
(Map.singleton 1 2) == (Map.singleton 2 3)
|
||||
Checking two maps for equality.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_equals =
|
||||
other = Map.empty . insert 1 "one" . insert 3 "three" . insert 5 "five"
|
||||
Examples.map == other
|
||||
== : Map -> Boolean
|
||||
== that = this.to_vector == that.to_vector
|
||||
|
||||
@ -141,8 +168,12 @@ type Map
|
||||
- value: The value to associate with `key`.
|
||||
|
||||
> Example
|
||||
Insert the value 3 into a map for the key 1.
|
||||
Map.empty.insert 1 3
|
||||
Insert the value "seven" into the map for the key 7.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_insert = Examples.map.insert 7 "seven"
|
||||
insert : Any -> Any -> Map
|
||||
insert key value = Internal.insert this key value
|
||||
|
||||
@ -153,8 +184,12 @@ type Map
|
||||
- key: The key to look up in the map.
|
||||
|
||||
> Example
|
||||
Get the value for the key 2 in a map.
|
||||
Map.empty.get 2
|
||||
Get the value for the key 1 in a map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_get = Examples.map.get 1
|
||||
get : Any -> Any ! Nothing
|
||||
get key =
|
||||
go map = case map of
|
||||
@ -173,9 +208,13 @@ type Map
|
||||
- other: The value to use if the key isn't present.
|
||||
|
||||
> Example
|
||||
Get the value for the key 2 in a map or instead return 10 if it isn't
|
||||
present.
|
||||
Map.empty.get_or_else 2 10
|
||||
Get the value for the key 2 in a map or instead return "zero" if it
|
||||
isn't present.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_get_or_else = Examples.map.get_or_else 2 "zero"
|
||||
get_or_else : Any -> Any -> Any
|
||||
get_or_else key ~other =
|
||||
this.get key . catch (_ -> other)
|
||||
@ -187,8 +226,13 @@ type Map
|
||||
value and returning a pair of `[key, value]`.
|
||||
|
||||
> Example
|
||||
Turn all keys into `Text` and double the values for a map `m`.
|
||||
m.transform (k -> v -> [k.to_text, v*2])
|
||||
Turn all keys into `Text` and append "_word" to the values in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_transform =
|
||||
Examples.map.transform (k -> v -> [k.to_text, v + "_word"])
|
||||
transform : (Any -> Any -> [Any, Any]) -> Map
|
||||
transform function =
|
||||
func_pairs = p -> function (p.at 0) (p.at 1)
|
||||
@ -202,8 +246,12 @@ type Map
|
||||
value and returning a value.
|
||||
|
||||
> Example
|
||||
Turn all values into `Text` for a map `m`.
|
||||
m.map (v -> v.to_text)
|
||||
Append "_word" to all values in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_map = Examples.map.map (+ "_word")
|
||||
map : (Any -> Any) -> Map
|
||||
map function =
|
||||
kv_func = _ -> function
|
||||
@ -217,8 +265,13 @@ type Map
|
||||
taking a key and a value and returning a value.
|
||||
|
||||
> Example
|
||||
Adding the key to the value in a map `m`.
|
||||
m.map_with_key (k -> v -> k + v)
|
||||
Prepend the keys to the values in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_map_with_key =
|
||||
Examples.map.map_with_key (k -> v -> k.to_text + "-" + v)
|
||||
map_with_key : (Any -> Any -> Any) -> Map
|
||||
map_with_key function =
|
||||
go map = case map of
|
||||
@ -234,8 +287,12 @@ type Map
|
||||
and returning a key.
|
||||
|
||||
> Example
|
||||
Doubling all keys in the map `m`.
|
||||
m.map_keys (k -> k*2)
|
||||
Doubling all keys in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_map_keys = Examples.map.map_keys *2
|
||||
map_keys : (Any -> Any) -> Map
|
||||
map_keys function =
|
||||
trans_function = k -> v -> [function k, v]
|
||||
@ -251,8 +308,12 @@ type Map
|
||||
computations with side-effects.
|
||||
|
||||
> Example
|
||||
Printing each value in the map `m`.
|
||||
m.each IO.println
|
||||
Printing each value in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_each = Examples.map.each IO.println
|
||||
each : (Any -> Any) -> Nothing
|
||||
each function =
|
||||
kv_func = _ -> function
|
||||
@ -268,8 +329,14 @@ type Map
|
||||
computations with side-effects.
|
||||
|
||||
> Example
|
||||
Printing each key and value in the map `m`.
|
||||
m.each (k -> v -> IO.println (k.to_text + v.to_text))
|
||||
Printing each key and value in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_each_with_key = Examples.map.each_with_key k->v->
|
||||
IO.println k
|
||||
IO.println v
|
||||
each_with_key : (Any -> Any -> Any) -> Nothing
|
||||
each_with_key function =
|
||||
go map = case map of
|
||||
@ -288,8 +355,12 @@ type Map
|
||||
- function: A binary function to apply to pairs of values in the map.
|
||||
|
||||
> Example
|
||||
Summing all of the values in the map `m`.
|
||||
m.fold 0 (+)
|
||||
Find the length of the longest word in the map.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_fold = Examples.map.fold 0 (l -> r -> Math.max l r.length)
|
||||
fold : Any -> (Any -> Any -> Any) -> Any
|
||||
fold init function =
|
||||
go map init = case map of
|
||||
@ -308,8 +379,13 @@ type Map
|
||||
current value, and combining them to yield a single value.
|
||||
|
||||
> Example
|
||||
Sum the keys and values in the map `m`.
|
||||
m.fold_with_key 0 (l -> k -> v -> l + k + v)
|
||||
Glue the values in the map together with the keys.
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_fold_with_key =
|
||||
Examples.map.fold_with_key "" (l -> k -> v -> l + k.to_text + v)
|
||||
fold_with_key : Any -> (Any -> Any -> Any -> Any) -> Any
|
||||
fold_with_key init function =
|
||||
go map init = case map of
|
||||
@ -324,7 +400,11 @@ type Map
|
||||
|
||||
> Example
|
||||
Get the keys from the map `m`.
|
||||
m.keys
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_keys = Examples.map.keys
|
||||
keys : Vector
|
||||
keys =
|
||||
builder = Vector.new_builder
|
||||
@ -342,7 +422,11 @@ type Map
|
||||
|
||||
> Example
|
||||
Get the values from the map `m`.
|
||||
m.values
|
||||
|
||||
import Standard.Base.Data.Map
|
||||
import Standard.Examples
|
||||
|
||||
example_values = Examples.map.values
|
||||
values : Vector
|
||||
values =
|
||||
builder = Vector.new_builder
|
||||
@ -355,3 +439,12 @@ type Map
|
||||
Tip -> Nothing
|
||||
to_vector_with_builder this
|
||||
builder.to_vector
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
An error for getting a missing value from a map.
|
||||
|
||||
Arguments:
|
||||
- key: The key that was asked for.
|
||||
type No_Value_For_Key key
|
||||
|
||||
|
@ -10,6 +10,13 @@ type Maybe
|
||||
|
||||
Arguments:
|
||||
- value: The contained value in the maybe.
|
||||
|
||||
> Example
|
||||
Construct a some value.
|
||||
|
||||
import Standard.Base.Data.Maybe
|
||||
|
||||
example_some = Maybe.Some "yes!"
|
||||
type Some value
|
||||
|
||||
## Applies the provided function to the contained value if it exists,
|
||||
@ -23,7 +30,10 @@ type Maybe
|
||||
|
||||
> Example
|
||||
Apply a function over a Some value to get 4.
|
||||
(Some 2).maybe 0 *2
|
||||
|
||||
import Standard.Base.Data.Maybe
|
||||
|
||||
example_maybe = Maybe.Some 2 . maybe 0 *2
|
||||
maybe : Any -> (Any -> Any) -> Any
|
||||
maybe ~default function = case this of
|
||||
Nothing -> default
|
||||
@ -33,7 +43,10 @@ type Maybe
|
||||
|
||||
> Example
|
||||
Check if `Nothing` is `Some`.
|
||||
Nothing.is_some
|
||||
|
||||
import Standard.Base.Data.Maybe
|
||||
|
||||
example_is_some = Maybe.Some "yes!" . is_some
|
||||
is_some : Boolean
|
||||
is_some = case this of
|
||||
Nothing -> False
|
||||
@ -43,7 +56,10 @@ type Maybe
|
||||
|
||||
> Example
|
||||
Check if `Nothing` is `Nothing`.
|
||||
Nothing.is_nothing
|
||||
|
||||
import Standard.Base.Data.Maybe
|
||||
|
||||
example_is_nothing = Maybe.Some "yes!" . is_nothing
|
||||
is_nothing : Boolean
|
||||
is_nothing = this.is_some.not
|
||||
|
||||
|
@ -55,7 +55,10 @@ type Deterministic_Random
|
||||
|
||||
> Example
|
||||
Step the generator with the input 1 and range 0 to 1
|
||||
Deterministic_Random.step 1 (Interval.inclusive 0 1)
|
||||
|
||||
from Standard.Base.Data.Noise.Generator import Deterministic_Random
|
||||
|
||||
example_det_random = Deterministic_Random.step 1 (Interval.inclusive 0 1)
|
||||
step : Number -> Interval -> Number
|
||||
step input interval =
|
||||
max_long = Long.MAX_VALUE
|
||||
|
@ -10,6 +10,7 @@ polyglot java import java.lang.String
|
||||
|
||||
> Example
|
||||
Calculate the inverse sine of 1.
|
||||
|
||||
1.asin
|
||||
Number.asin : Decimal
|
||||
Number.asin = Math.asin this.to_decimal
|
||||
@ -20,6 +21,7 @@ Number.asin = Math.asin this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the inverse cosine of 1.
|
||||
|
||||
1.acos
|
||||
Number.acos : Decimal
|
||||
Number.acos = Math.acos this.to_decimal
|
||||
@ -30,7 +32,8 @@ Number.acos = Math.acos this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the inverse tangent of 1.
|
||||
1.acos
|
||||
|
||||
1.atan
|
||||
Number.atan : Decimal
|
||||
Number.atan = Math.atan this.to_decimal
|
||||
|
||||
@ -44,6 +47,7 @@ Number.atan = Math.atan this.to_decimal
|
||||
|
||||
> Example
|
||||
Convert the coordinates 1 and 2 to polar form.
|
||||
|
||||
1.atan_2 2
|
||||
Number.atan_2 : Number -> Decimal
|
||||
Number.atan_2 y = Math.atan2 this.to_decimal y.to_decimal
|
||||
@ -52,6 +56,7 @@ Number.atan_2 y = Math.atan2 this.to_decimal y.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the sine of 2.
|
||||
|
||||
2.sin
|
||||
Number.sin : Decimal
|
||||
Number.sin = Math.sin this.to_decimal
|
||||
@ -60,6 +65,7 @@ Number.sin = Math.sin this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the cosine of 2.
|
||||
|
||||
2.cos
|
||||
Number.cos : Decimal
|
||||
Number.cos = Math.cos this.to_decimal
|
||||
@ -68,6 +74,7 @@ Number.cos = Math.cos this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the tangent of 2.
|
||||
|
||||
2.tan
|
||||
Number.tan : Decimal
|
||||
Number.tan = Math.tan this.to_decimal
|
||||
@ -76,6 +83,7 @@ Number.tan = Math.tan this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the hyperbolic sine of 1.
|
||||
|
||||
1.sinh
|
||||
Number.sinh : Decimal
|
||||
Number.sinh = Math.sinh this.to_decimal
|
||||
@ -84,6 +92,7 @@ Number.sinh = Math.sinh this.to_decimal
|
||||
|
||||
> Example
|
||||
Calcualte the hyperbolic cosine of 1.
|
||||
|
||||
1.cosh
|
||||
Number.cosh : Decimal
|
||||
Number.cosh = Math.cosh this.to_decimal
|
||||
@ -92,6 +101,7 @@ Number.cosh = Math.cosh this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the hyperbolic tangent of 1.
|
||||
|
||||
1.tanh
|
||||
Number.tanh : Decimal
|
||||
Number.tanh = Math.tanh this.to_decimal
|
||||
@ -101,6 +111,7 @@ Number.tanh = Math.tanh this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate e to the 4th power.
|
||||
|
||||
4.exp
|
||||
Number.exp : Decimal
|
||||
Number.exp = Math.exp this.to_decimal
|
||||
@ -109,6 +120,7 @@ Number.exp = Math.exp this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the natural logarithm of 2.
|
||||
|
||||
2.ln
|
||||
Number.ln : Decimal
|
||||
Number.ln = Math.log this.to_decimal
|
||||
@ -117,6 +129,7 @@ Number.ln = Math.log this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate the square root of 8.
|
||||
|
||||
8.sqrt
|
||||
Number.sqrt : Decimal
|
||||
Number.sqrt = Math.sqrt this.to_decimal
|
||||
@ -128,13 +141,14 @@ Number.sqrt = Math.sqrt this.to_decimal
|
||||
|
||||
> Example
|
||||
Calculate log 2 of 4.
|
||||
|
||||
4.log 2
|
||||
Number.log : Number -> Decimal
|
||||
Number.log base = this.ln / base.ln
|
||||
|
||||
## UNSTABLE The API will become more user-friendly in future.
|
||||
## UNSTABLE This API is not user-friendly and will be improved in the future.
|
||||
|
||||
Converts a decimal value to a string, using the Java string formatting
|
||||
Converts a numeric value to a string, using the Java string formatting
|
||||
syntax.
|
||||
|
||||
Arguments:
|
||||
@ -142,9 +156,10 @@ Number.log base = this.ln / base.ln
|
||||
|
||||
> Example
|
||||
Convert the value 5 to a string.
|
||||
5.fmt "%d"
|
||||
Decimal.format : Text -> Text
|
||||
Decimal.format fmt = String.format fmt this
|
||||
|
||||
5.format "%x"
|
||||
Number.format : Text -> Text
|
||||
Number.format fmt = String.format fmt this
|
||||
|
||||
## Creates a new right-exclusive range of integers from `this` to `n`.
|
||||
|
||||
@ -152,8 +167,9 @@ Decimal.format fmt = String.format fmt this
|
||||
- n: The end of the range.
|
||||
|
||||
> Example
|
||||
Create a range from 1 to 5.
|
||||
1.up_to 5
|
||||
Create a range containing the numbers 0, 1, 2, 3, 4.
|
||||
|
||||
0.up_to 5
|
||||
Integer.up_to : Integer -> Range
|
||||
Integer.up_to n = Range this n
|
||||
|
||||
@ -166,6 +182,7 @@ Integer.up_to n = Range this n
|
||||
|
||||
> Example
|
||||
Check if 1 is equal to 1.0000001 within 0.001.
|
||||
|
||||
1.equals 1.0000001 epsilon=0.001
|
||||
Number.equals : Number -> Number -> Boolean
|
||||
Number.equals that epsilon=0.0 = (this - that).abs <= epsilon
|
||||
@ -175,8 +192,14 @@ Number.equals that epsilon=0.0 = (this - that).abs <= epsilon
|
||||
Arguments:
|
||||
- that: The number to compare `this` against.
|
||||
|
||||
? Math.min or Number.min
|
||||
While we provide the min method on `Number`, we find it more intuitive to
|
||||
write `Math.min a b` rather than `a.min b`. To that end, we recommend using
|
||||
the first style.
|
||||
|
||||
> Example
|
||||
Find the minimum of 2 and 5.
|
||||
|
||||
2.min 5
|
||||
Number.min : Number -> Number
|
||||
Number.min that = if this < that then this else that
|
||||
@ -186,8 +209,14 @@ Number.min that = if this < that then this else that
|
||||
Arguments:
|
||||
- that: The number to compare `this` against.
|
||||
|
||||
? Math.max or Number.max
|
||||
While we provide the max method on `Number`, we find it more intuitive to
|
||||
write `Math.max a b` rather than `a.max b`. To that end, we recommend using
|
||||
the first style.
|
||||
|
||||
> Example
|
||||
Find the maximum of 2 and 5.
|
||||
|
||||
2.max 5
|
||||
Number.max : Number -> Number
|
||||
Number.max that = if this > that then this else that
|
||||
@ -196,6 +225,7 @@ Number.max that = if this > that then this else that
|
||||
|
||||
> Example
|
||||
Convert the number 8 to JSON.
|
||||
|
||||
8.to_json
|
||||
Number.to_json : Json.Number
|
||||
Number.to_json = Json.Number this
|
||||
@ -208,6 +238,7 @@ Number.to_json = Json.Number this
|
||||
|
||||
> Example
|
||||
Parse the text "7.6" into a decimal number.
|
||||
|
||||
Decimal.parse 7.6
|
||||
Decimal.parse : Text -> Decimal ! Nothing
|
||||
Decimal.parse text =
|
||||
|
@ -20,7 +20,10 @@ type Ordering
|
||||
|
||||
> Example
|
||||
Converting equal ordering to a signed number.
|
||||
Ordering.Equal.to_sign
|
||||
|
||||
import Standard.Base.Data.Ordering
|
||||
|
||||
example_to_sign = Ordering.Equal.to_sign
|
||||
to_sign : Integer
|
||||
to_sign = case this of
|
||||
Less -> -1
|
||||
|
@ -4,8 +4,18 @@ from Standard.Base import all
|
||||
type Sort_Order
|
||||
|
||||
## Elements should be sorted in ascending order.
|
||||
|
||||
> Example
|
||||
Create an ascending order.
|
||||
|
||||
Sort_Order.Ascending
|
||||
type Ascending
|
||||
|
||||
## Elements should be sorted in descending order.
|
||||
|
||||
> Example
|
||||
Create a descending order.
|
||||
|
||||
Sort_Order.Descending
|
||||
type Descending
|
||||
|
||||
|
@ -15,23 +15,26 @@ type Range
|
||||
|
||||
> Example
|
||||
The following range has 100 elements.
|
||||
0.up_to 100
|
||||
|
||||
0.up_to 100 . length
|
||||
length : Number
|
||||
length = this.end - this.start
|
||||
|
||||
## Checks if this range is empty.
|
||||
|
||||
> Example
|
||||
0.up_to 0 . is_empty == True
|
||||
0.up_to 100 . is_empty == False
|
||||
Checking if the range from 0 to 100 is empty.
|
||||
|
||||
0.up_to 100 . is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = this.end <= this.start
|
||||
|
||||
## Checks if this range is not empty.
|
||||
|
||||
> Example
|
||||
0.up_to 0 . not_empty == False
|
||||
0.up_to 100 . not_empty == True
|
||||
Checking if the range from 0 to 100 is not empty.
|
||||
|
||||
0.up_to 100 . not_empty
|
||||
not_empty : Boolean
|
||||
not_empty = this.is_empty.not
|
||||
|
||||
@ -55,8 +58,8 @@ type Range
|
||||
- function: The function to apply to each integer in the range.
|
||||
|
||||
> Example
|
||||
To print all the numbers from 1 to 100 use:
|
||||
1.up_to 101 . each IO.println
|
||||
To print all the numbers from 1 to 10 use:
|
||||
1.up_to 11 . each IO.println
|
||||
each : (Number -> Any) -> Nothing
|
||||
each function =
|
||||
it start end = if start == end then Nothing else
|
||||
@ -79,9 +82,10 @@ type Range
|
||||
f (...(f (f init start) start+1)...) end-1
|
||||
|
||||
> Example
|
||||
In the following example, we'll compute the sum of all elements of a
|
||||
range:
|
||||
Range 0 100 . fold 0 (+)
|
||||
In the following example, we'll compute the sum of all integers less
|
||||
than 100.
|
||||
|
||||
0.up_to 100 . fold 0 (+)
|
||||
fold : Any -> (Any -> Number -> Any) -> Any
|
||||
fold init function =
|
||||
it acc start end = if start == end then acc else
|
||||
@ -99,6 +103,7 @@ type Range
|
||||
|
||||
> Example
|
||||
Checking that all numbers in the range are greater than 5.
|
||||
|
||||
10.up_to 100 . all (> 5)
|
||||
all : (Number -> Boolean) -> Boolean
|
||||
all predicate =
|
||||
@ -117,6 +122,7 @@ type Range
|
||||
|
||||
> Example
|
||||
Checking that at least one number in the range is greater than 10.
|
||||
|
||||
1.up_to 100 . exists (> 10)
|
||||
exists : (Number -> Boolean) -> Boolean
|
||||
exists predicate =
|
||||
@ -134,6 +140,7 @@ type Range
|
||||
|
||||
> Example
|
||||
Checking that at least one number in the range is greater than 10.
|
||||
|
||||
1.up_to 100 . any (> 10)
|
||||
any : (Number -> Boolean) -> Boolean
|
||||
any predicate = this.exists predicate
|
||||
@ -142,7 +149,8 @@ type Range
|
||||
|
||||
> Example
|
||||
Getting a vector of the numbers 1 to 5.
|
||||
1.up_to 6 . to_vector == [1, 2, 3, 4, 5]
|
||||
|
||||
1.up_to 6 . to_vector
|
||||
to_vector : Vector.Vector
|
||||
to_vector =
|
||||
length = Math.max 0 (this.end - this.start)
|
||||
|
@ -7,6 +7,8 @@ import Standard.Base.Meta
|
||||
|
||||
from Builtins export Text
|
||||
|
||||
export Standard.Base.Data.Text.Split_Kind
|
||||
|
||||
polyglot java import com.ibm.icu.lang.UCharacter
|
||||
polyglot java import com.ibm.icu.text.BreakIterator
|
||||
polyglot java import org.enso.base.Text_Utils
|
||||
@ -20,6 +22,7 @@ polyglot java import org.enso.base.Text_Utils
|
||||
|
||||
> Example
|
||||
Getting the length of the string "건반(Korean)".
|
||||
|
||||
"건반(Korean)".length
|
||||
Text.length : Integer
|
||||
Text.length =
|
||||
@ -45,6 +48,7 @@ Text.length =
|
||||
|
||||
> Example
|
||||
Print each character in the text "aaa".
|
||||
|
||||
"aaa".each IO.println
|
||||
Text.each : (Text -> Any) -> Nothing
|
||||
Text.each function =
|
||||
@ -70,8 +74,9 @@ Text.each function =
|
||||
|
||||
> Example
|
||||
Get the individual characters in the text "건반(Korean)".
|
||||
|
||||
"건반(Korean)".characters
|
||||
Text.characters : Vector.Vector
|
||||
Text.characters : Vector.Vector Text
|
||||
Text.characters =
|
||||
bldr = Vector.new_builder
|
||||
this.each bldr.append
|
||||
@ -85,12 +90,14 @@ Text.characters =
|
||||
|
||||
> Example
|
||||
Split the comma-separated text into a vector of items.
|
||||
|
||||
"ham,eggs,cheese,tomatoes".split ","
|
||||
|
||||
> Example
|
||||
Split the string on whitespace into a vector of items.
|
||||
|
||||
"ham eggs cheese tomatoes".split Split_Kind.Whitespace
|
||||
Text.split : Split_Kind -> Vector.Vector
|
||||
Text.split : Split_Kind -> Vector.Vector Text
|
||||
Text.split (separator = Split_Kind.Whitespace) =
|
||||
result = case separator of
|
||||
Split_Kind.Whitespace -> Text_Utils.split_on_whitespace this
|
||||
@ -113,8 +120,9 @@ Text.split (separator = Split_Kind.Whitespace) =
|
||||
|
||||
> Example
|
||||
Getting the words in the sentence "I have not one, but two cats."
|
||||
|
||||
"I have not one, but two cats.".words
|
||||
Text.words : Boolean -> Vector.Vector
|
||||
Text.words : Boolean -> Vector.Vector Text
|
||||
Text.words keep_whitespace=False =
|
||||
iterator = BreakIterator.getWordInstance
|
||||
iterator.setText this
|
||||
@ -151,6 +159,7 @@ Text.words keep_whitespace=False =
|
||||
The string 'é' (i.e. the character U+00E9, LATIN SMALL LETTER E WITH ACUTE)
|
||||
is canonically the same as the string 'e\u0301' (i.e. the letter `e`
|
||||
followed by U+0301, COMBINING ACUTE ACCENT). Therefore:
|
||||
|
||||
('é' == 'e\u0301') == True
|
||||
Text.== : Any -> Boolean
|
||||
Text.== that = if Meta.is_same_object this Text then Meta.is_same_object that Text else
|
||||
@ -176,6 +185,7 @@ Text.== that = if Meta.is_same_object this Text then Meta.is_same_object that Te
|
||||
LATIN SMALL LETTER E WITH ACUTE), which is canonically the same as the
|
||||
string 'e\u0301' (i.e. the letter `e` followed by U+0301, COMBINING ACUTE
|
||||
ACCENT). Therefore:
|
||||
|
||||
(('É' . equals_ignore_case 'é') && ('é' == 'e\u0301')) == True
|
||||
Text.equals_ignore_case : Text -> Boolean
|
||||
Text.equals_ignore_case that = Text_Utils.equals_ignore_case this that
|
||||
@ -187,6 +197,7 @@ Text.equals_ignore_case that = Text_Utils.equals_ignore_case this that
|
||||
|
||||
> Example
|
||||
Checking how "a" orders in relation to "b".
|
||||
|
||||
"a".compare_to "b"
|
||||
Text.compare_to : Text -> Ordering
|
||||
Text.compare_to that = if this == that then Ordering.Equal else
|
||||
@ -199,6 +210,7 @@ Text.compare_to that = if this == that then Ordering.Equal else
|
||||
|
||||
> Example
|
||||
Check if the text "aaa" is empty.
|
||||
|
||||
"aaa".is_empty
|
||||
Text.is_empty : Boolean
|
||||
Text.is_empty = this == ""
|
||||
@ -210,6 +222,7 @@ Text.is_empty = this == ""
|
||||
|
||||
> Example
|
||||
Check if the text "aaa" is not empty.
|
||||
|
||||
"aaa".not_empty
|
||||
Text.not_empty : Boolean
|
||||
Text.not_empty = this.is_empty.not
|
||||
@ -222,8 +235,9 @@ Text.not_empty = this.is_empty.not
|
||||
|
||||
> Example
|
||||
Get the UTF-8 bytes of the text "Hello".
|
||||
|
||||
"Hello".utf_8
|
||||
Text.utf_8 : Vector.Vector
|
||||
Text.utf_8 : Vector.Vector Byte
|
||||
Text.utf_8 = Vector.from_array (Text_Utils.get_bytes this)
|
||||
|
||||
## Takes a vector of bytes and returns Text resulting from decoding it as UTF-8.
|
||||
@ -236,8 +250,9 @@ Text.utf_8 = Vector.from_array (Text_Utils.get_bytes this)
|
||||
|
||||
> Example
|
||||
Decoding the bytes to get a text.
|
||||
|
||||
Text.from_utf_8 [-32, -92, -107, -32, -91, -115, -32, -92, -73, -32, -92, -65]
|
||||
Text.from_utf_8 : Vector.Vector -> Text
|
||||
Text.from_utf_8 : Vector.Vector Byte -> Text
|
||||
Text.from_utf_8 bytes = Text_Utils.from_utf_8 bytes.to_array
|
||||
|
||||
## Returns a vector containing integers representing the Unicode codepoints of
|
||||
@ -248,8 +263,9 @@ Text.from_utf_8 bytes = Text_Utils.from_utf_8 bytes.to_array
|
||||
|
||||
> Example
|
||||
Get the codepoints of the text "Hello".
|
||||
|
||||
"Hello".codepoints
|
||||
Text.codepoints : Vector.Vector
|
||||
Text.codepoints : Vector.Vector Integer
|
||||
Text.codepoints = Vector.from_array (Text_Utils.get_codepoints this)
|
||||
|
||||
## Takes an array of numbers and returns the text resulting from interpreting it
|
||||
@ -261,7 +277,7 @@ Text.codepoints = Vector.from_array (Text_Utils.get_codepoints this)
|
||||
> Example
|
||||
Converting a vector of codepoints back into a text.
|
||||
Text.from_codepoints [129318, 127996, 8205, 9794, 65039]
|
||||
Text.from_codepoints : Vector.Vector -> Text
|
||||
Text.from_codepoints : Vector.Vector Integer -> Text
|
||||
Text.from_codepoints codepoints = Text_Utils.from_codepoints codepoints.to_array
|
||||
|
||||
## Checks whether `this` starts with `prefix`.
|
||||
@ -277,6 +293,7 @@ Text.from_codepoints codepoints = Text_Utils.from_codepoints codepoints.to_array
|
||||
|
||||
> Example
|
||||
See if the text "Hello" starts with the prefix "hi".
|
||||
|
||||
"Hello".starts_with "hi"
|
||||
Text.starts_with : Text -> Boolean
|
||||
Text.starts_with prefix = Text_Utils.starts_with this prefix
|
||||
@ -311,6 +328,7 @@ Text.ends_with suffix = Text_Utils.ends_with this suffix
|
||||
|
||||
> Example
|
||||
See if the text "Hello" contains the text "ell".
|
||||
|
||||
"Hello".contains "ell"
|
||||
Text.contains : Text -> Boolean
|
||||
Text.contains sequence = Text_Utils.contains this sequence
|
||||
@ -325,14 +343,17 @@ Text.contains sequence = Text_Utils.contains this sequence
|
||||
|
||||
> Example
|
||||
Replace letters in the text "aaa".
|
||||
|
||||
'aaa'.replace 'aa' 'b' == 'ba'
|
||||
Text.replace : Text -> Text -> Text
|
||||
Text.replace old_sequence new_sequence = Text_Utils.replace this old_sequence new_sequence
|
||||
Text.replace old_sequence new_sequence =
|
||||
Text_Utils.replace this old_sequence new_sequence
|
||||
|
||||
## Text to JSON conversion.
|
||||
|
||||
> Example
|
||||
Convert the text "cześć" to JSON.
|
||||
|
||||
"cześć".to_json
|
||||
Text.to_json : Json.String
|
||||
Text.to_json = Json.String this
|
||||
@ -346,6 +367,7 @@ Text.to_json = Json.String this
|
||||
|
||||
> Example
|
||||
Repeat the string "ABBA" five times.
|
||||
|
||||
"ABBA".repeat 5
|
||||
Text.repeat : Integer -> Text
|
||||
Text.repeat count =
|
||||
@ -365,6 +387,7 @@ Text.repeat count =
|
||||
|
||||
> Example
|
||||
Removing the first three characters from the text "ABBA".
|
||||
|
||||
"ABBA".drop_first 3
|
||||
Text.drop_first : Integer -> Text
|
||||
Text.drop_first count =
|
||||
@ -388,6 +411,7 @@ Text.drop_first count =
|
||||
|
||||
> Example
|
||||
Removing the last three characters from the text "ABBA".
|
||||
|
||||
"ABBA".drop_last 3
|
||||
Text.drop_last : Integer -> Text
|
||||
Text.drop_last count =
|
||||
@ -410,6 +434,7 @@ Text.drop_last count =
|
||||
|
||||
> Example
|
||||
Make a new text from the first two characters of "boo".
|
||||
|
||||
"boo".take_first 2
|
||||
Text.take_first : Integer -> Text
|
||||
Text.take_first count =
|
||||
@ -432,6 +457,7 @@ Text.take_first count =
|
||||
|
||||
> Example
|
||||
Make a new text from the last two characters of "boo".
|
||||
|
||||
"boo".take_last 2
|
||||
Text.take_last : Integer -> Text
|
||||
Text.take_last count =
|
||||
@ -454,11 +480,16 @@ Text.take_last count =
|
||||
|
||||
> Example
|
||||
Converting a text to lower case in the default locale:
|
||||
|
||||
"My TeXt!".to_lower_case == "my text!"
|
||||
|
||||
> Example
|
||||
Converting a text to lower case in a specified locale:
|
||||
"I".to_lower_case (Locale.new "tr") == "ı"
|
||||
Converting a text to lower case in a specified locale (here, Turkey):
|
||||
|
||||
from Standard.Base import all
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_case_with_locale = "I".to_lower_case (Locale.new "tr") == "ı"
|
||||
Text.to_lower_case : Locale.Locale -> Text
|
||||
Text.to_lower_case locale=Locale.default =
|
||||
UCharacter.toLowerCase locale.java_locale this
|
||||
@ -476,11 +507,17 @@ Text.to_lower_case locale=Locale.default =
|
||||
|
||||
> Example
|
||||
Converting a text to upper case in the default locale:
|
||||
|
||||
"My TeXt!".to_lower_case == "my text!"
|
||||
|
||||
> Example
|
||||
Converting a text to upper case in a specified locale:
|
||||
"i".to_upper_case (Locale.new "tr") . should_equal "İ"
|
||||
|
||||
from Standard.Base import all
|
||||
import Standard.Base.Data.Locale
|
||||
|
||||
example_case_with_locale = "i".to_upper_case (Locale.new "tr") == "İ"
|
||||
Text.to_upper_case : Locale.Locale -> Text
|
||||
Text.to_upper_case locale=Locale.default =
|
||||
UCharacter.toUpperCase locale.java_locale this
|
||||
|
||||
|
@ -10,19 +10,145 @@ polyglot java import java.time.format.DateTimeFormatter
|
||||
polyglot java import java.time.ZonedDateTime
|
||||
polyglot java import org.enso.base.Time_Utils
|
||||
|
||||
type Time_Error
|
||||
## Obtains the current date-time from the system clock in the system timezone.
|
||||
|
||||
## UNSTABLE
|
||||
> Example
|
||||
Get the current time
|
||||
|
||||
An error produced while working with time.
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
Arguments:
|
||||
- error_message: The message for the error.
|
||||
type Time_Error error_message
|
||||
example_now = Time.now
|
||||
now : Time
|
||||
now = Time ZonedDateTime.now
|
||||
|
||||
## Obtains an instance of `Time` from a year, month, day, hour, minute,
|
||||
second, nanosecond and timezone.
|
||||
|
||||
Arguments:
|
||||
- month: the month-of-year to represent, from 1 (January) to 12 (December)
|
||||
- day: the day-of-month to represent, from 1 to 31 and must be valid for the
|
||||
year and month
|
||||
- hour: the hour-of-day to represent, from 0 to 23
|
||||
- minute: the minute-of-hour to represent, from 0 to 59
|
||||
- second: the second-of-minute to represent, from 0 to 59
|
||||
- nanosecond: the nano-of-second to represent, from 0 to 999,999,999
|
||||
- zone: the timezone
|
||||
|
||||
Returns a `Time_Error` if the provided time cannot be represented.
|
||||
|
||||
> Example
|
||||
Create a new zoned date time at Unix epoch.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_new = Time.new 1970 (zone = Zone.utc)
|
||||
|
||||
> Example
|
||||
Get the 5 August 1986 at midnight.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_new = Time.new 1986 8 5
|
||||
new : Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Zone -> Time ! Time_Error
|
||||
new year (month = 1) (day = 1) (hour = 0) (minute = 0) (second = 0) (nanosecond = 0) (zone = Zone.system) =
|
||||
Panic.recover (Time (ZonedDateTime.of year month day hour minute second nanosecond zone.internal_zone_id)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Obtains an instance of `Time` from a text such as
|
||||
"2007-12-03T10:15:30+01:00 Europe/Paris".
|
||||
|
||||
Arguments:
|
||||
- text: The text representing the time to be parsed.
|
||||
- pattern: The pattern to use for parsing the input text.
|
||||
- locale: The locale in which the pattern should be interpreted.
|
||||
|
||||
? Pattern Syntax
|
||||
For the list of accepted symbols in pattern refer to `Time.format` doc.
|
||||
|
||||
? Default Time Format
|
||||
The text must represent a valid date-time and is parsed using the ISO-8601
|
||||
extended offset date-time format to add the timezone. The section in square
|
||||
brackets is not part of the ISO-8601 standard. The format consists of:
|
||||
|
||||
- The ISO offset date time.
|
||||
- If the zone ID is not available or is a zone offset then the format is
|
||||
complete.
|
||||
- An open square bracket '['.
|
||||
- The zone ID. This is not part of the ISO-8601 standard. Parsing is case
|
||||
sensitive.
|
||||
- A close square bracket ']'.
|
||||
|
||||
This method will return a `Time_Error` if the provided time cannot be parsed
|
||||
using the above format.
|
||||
|
||||
> Example
|
||||
Parse UTC time.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Time.parse "2020-10-01T04:11:12Z"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Time.parse "2020-10-01T04:11:12-04:00"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time specifying New York timezone.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Time.parse "2020-10-01T04:11:12-04:00[America/New_York]"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time with nanoseconds.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Time.parse "2020-10-01T04:11:12.177528-04:00"
|
||||
|
||||
> Example
|
||||
Recover from the parse error.
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Time.parse "2020-10-01" . catch e-> case e of
|
||||
Time.Error _ -> Time.now
|
||||
|
||||
> Example
|
||||
Parse "2020-05-06 04:30:20" as Time
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse = Date.parse "2020-05-06 04:30:20" "yyyy-MM-dd HH:mm:ss"
|
||||
|
||||
> Example
|
||||
Parse "06 of May 2020 at 04:30AM" as Time
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse =
|
||||
Date.parse "06 of May 2020 at 04:30AM" "dd 'of' MMMM yyyy 'at' hh:mma"
|
||||
parse : Text -> Text | Nothing -> Locale -> Time ! Time_Error
|
||||
parse text pattern=Nothing locale=Locale.default =
|
||||
result = Panic.recover <| case pattern of
|
||||
Nothing -> Time_Utils.parse_time text
|
||||
Text -> Time_Utils.parse_time_format text pattern locale.java_locale
|
||||
_ -> Panic.throw (Time_Error "An invalid pattern was provided.")
|
||||
Time result . map_error <| case _ of
|
||||
Polyglot_Error err -> Time_Error err.getMessage
|
||||
x -> x
|
||||
|
||||
type Time
|
||||
|
||||
## A date-time with a timezone in the ISO-8601 calendar system, such as
|
||||
## PRIVATE
|
||||
|
||||
A date-time with a timezone in the ISO-8601 calendar system, such as
|
||||
"2007-12-03T10:15:30+01:00 Europe/Paris".
|
||||
|
||||
Arguments:
|
||||
@ -41,7 +167,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current year.
|
||||
Time.now.year
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_year = Time.now.year
|
||||
year : Integer
|
||||
year = this . internal_zoned_date_time . getYear
|
||||
|
||||
@ -49,7 +178,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current month.
|
||||
Time.now.month
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_month = Time.now.month
|
||||
month : Integer
|
||||
month = this . internal_zoned_date_time . getMonthValue
|
||||
|
||||
@ -57,7 +189,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current day.
|
||||
Time.now.day
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_day = Time.now.day
|
||||
day : Integer
|
||||
day = this . internal_zoned_date_time . getDayOfMonth
|
||||
|
||||
@ -65,7 +200,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current hour.
|
||||
Time.now.hour
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_hour = Time.now.hour
|
||||
hour : Integer
|
||||
hour = this . internal_zoned_date_time . getHour
|
||||
|
||||
@ -73,7 +211,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current minute.
|
||||
Time.now.minute
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_minute = Time.now.minute
|
||||
minute : Integer
|
||||
minute = this . internal_zoned_date_time . getMinute
|
||||
|
||||
@ -81,7 +222,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current second.
|
||||
Time.now.second
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_second = Time.now.second
|
||||
second : Integer
|
||||
second = this . internal_zoned_date_time . getSecond
|
||||
|
||||
@ -89,7 +233,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current nanosecond.
|
||||
Time.now.nanosecond
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_nanosecond = Time.now.nanosecond
|
||||
nanosecond : Integer
|
||||
nanosecond = this . internal_zoned_date_time . getNano
|
||||
|
||||
@ -97,7 +244,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current timezone.
|
||||
Time.now.zone
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_zone = Time.now.zone
|
||||
zone : Zone
|
||||
zone = Zone.zone (this . internal_zoned_date_time . getZone)
|
||||
|
||||
@ -105,7 +255,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current number of seconds from the Unix epoch.
|
||||
Time.now.to_epoch_seconds
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_epoch = Time.now.to_epoch_seconds
|
||||
to_epoch_seconds : Integer
|
||||
to_epoch_seconds = this . internal_zoned_date_time . toEpochSecond
|
||||
|
||||
@ -113,7 +266,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Get the current number of milliseconds from the unix epoch.
|
||||
Time.now.to_epoch_milliseconds
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_epoch = Time.now.to_epoch_milliseconds
|
||||
to_epoch_milliseconds : Integer
|
||||
to_epoch_milliseconds = this . internal_zoned_date_time . toInstant . toEpochMilli
|
||||
|
||||
@ -122,7 +278,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Convert the current time to a time of day.
|
||||
Time.now.time_of_day
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_time_of_day = Time.now.time_of_day
|
||||
time_of_day : Time_Of_Day
|
||||
time_of_day = Time_Of_Day.time_of_day this.internal_zoned_date_time.toLocalTime
|
||||
|
||||
@ -131,7 +290,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Convert the current time to a date.
|
||||
Time.now.date
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_date = Time.now.date
|
||||
date : Date
|
||||
date = Date.date this.internal_zoned_date_time.toLocalDate
|
||||
|
||||
@ -142,7 +304,11 @@ type Time
|
||||
|
||||
> Example
|
||||
Convert time instance to -04:00 timezone.
|
||||
Time.new 2020 . at_zone (Zone.new -4)
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
exaomple_at_zone = Time.new 2020 . at_zone (Zone.new -4)
|
||||
at_zone : Zone -> Time
|
||||
at_zone zone = Time (this.internal_zoned_date_time . withZoneSameInstant zone.internal_zone_id)
|
||||
|
||||
@ -151,13 +317,13 @@ type Time
|
||||
Arguments:
|
||||
- amount: The amount of time to add to this instant.
|
||||
|
||||
> Example
|
||||
Add 1 hour to a zoned date time.
|
||||
Time.new 2020 + 1.hour
|
||||
|
||||
> Example
|
||||
Add 15 years and 3 hours to a zoned date time.
|
||||
Time.new 2020 + 15.years + 3.hours
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_plus = Time.new 2020 + 15.years + 3.hours
|
||||
+ : Duration -> Time
|
||||
+ amount = Time (this . internal_zoned_date_time . plus amount.internal_period . plus amount.internal_duration)
|
||||
|
||||
@ -167,13 +333,13 @@ type Time
|
||||
Arguments:
|
||||
- amount: The amount of time to subtract from this instant.
|
||||
|
||||
> Example
|
||||
Subtract 10 days from a zoned date time.
|
||||
Time.new 2020 - 10.days
|
||||
|
||||
> Example
|
||||
Subtract 1 year and 9 months from a zoned date time.
|
||||
Time.new 2020 - 1.year - 9.months
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_minus = Time.new 2020 - 1.year - 9.months
|
||||
- : Duration -> Time
|
||||
- amount = Time (this . internal_zoned_date_time . minus amount.internal_period . minus amount.internal_duration)
|
||||
|
||||
@ -181,7 +347,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Convert the current time to text.
|
||||
Time.now.to_text
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_to_text = Time.now.to_text
|
||||
to_text : Text
|
||||
to_text = Time_Utils.default_time_formatter . format this.internal_zoned_date_time
|
||||
|
||||
@ -189,7 +358,10 @@ type Time
|
||||
|
||||
> Example
|
||||
Convert the current time to JSON.
|
||||
Time.now.to_json
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_to_json = Time.now.to_json
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Time"], ["year", this.year], ["month", this.month], ["day", this.day], ["hour", this.hour], ["minute", this.minute], ["second", this.second], ["nanosecond", this.nanosecond], ["zone", this.zone]]
|
||||
|
||||
@ -245,125 +417,40 @@ type Time
|
||||
The count of pattern letters determines the format.
|
||||
|
||||
> Example
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as "2020-10-08T16:41:13+03:00[Europe/Moscow]"
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "yyyy-MM-dd'T'HH:mm:ssZZZZ'['VV']'"
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as
|
||||
"2020-10-08T16:41:13+03:00[Europe/Moscow]".
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_format =
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "yyyy-MM-dd'T'HH:mm:ssZZZZ'['VV']'"
|
||||
|
||||
> Example
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as "Thursday October 8 4:41 PM"
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "EEEE MMMM d h:mm a"
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as
|
||||
"Thursday October 8 4:41 PM".
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_format =
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "EEEE MMMM d h:mm a"
|
||||
|
||||
> Example
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as "Thu Oct 8 (16:41)"
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "EEE MMM d (HH:mm)"
|
||||
Format "2020-10-08T16:41:13+03:00[Europe/Moscow]" as
|
||||
"Thu Oct 8 (16:41)".
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_format =
|
||||
Time.parse "2020-10-08T16:41:13+03:00[Europe/Moscow]" . format "EEE MMM d (HH:mm)"
|
||||
format : Text -> Text
|
||||
format pattern = DateTimeFormatter.ofPattern pattern . format this.internal_zoned_date_time
|
||||
|
||||
## Obtains an instance of `Time` from a text such as
|
||||
"2007-12-03T10:15:30+01:00 Europe/Paris".
|
||||
type Time_Error
|
||||
|
||||
Arguments:
|
||||
- text: The text representing the time to be parsed.
|
||||
## UNSTABLE
|
||||
|
||||
? Valid Formatting
|
||||
The text must represent a valid date-time and is parsed using the ISO-8601
|
||||
extended offset date-time format to add the timezone. The section in square
|
||||
brackets is not part of the ISO-8601 standard. The format consists of:
|
||||
An error produced while working with time.
|
||||
|
||||
- The ISO offset date time.
|
||||
- If the zone ID is not available or is a zone offset then the format is
|
||||
complete.
|
||||
- An open square bracket '['.
|
||||
- The zone ID. This is not part of the ISO-8601 standard. Parsing is case
|
||||
sensitive.
|
||||
- A close square bracket ']'.
|
||||
Arguments:
|
||||
- error_message: The message for the error.
|
||||
type Time_Error error_message
|
||||
|
||||
This method will return a `Time_Error` if the provided time cannot be parsed
|
||||
using the above format.
|
||||
|
||||
> Example
|
||||
Parse UTC time.
|
||||
Time.parse "2020-10-01T04:11:12Z"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time.
|
||||
Time.parse "2020-10-01T04:11:12-04:00"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time specifying New York timezone.
|
||||
Time.parse "2020-10-01T04:11:12-04:00[America/New_York]"
|
||||
|
||||
> Example
|
||||
Parse UTC-04:00 time with nanoseconds.
|
||||
Time.parse "2020-10-01T04:11:12.177528-04:00"
|
||||
|
||||
> Example
|
||||
Recover from the parse error.
|
||||
Time.parse "2020-10-01" . catch e-> case e of
|
||||
Time.Error _ -> Time.now
|
||||
parse : Text -> Time ! Time_Error
|
||||
parse text =
|
||||
Panic.recover (Time (Time_Utils.parse_time text)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Converts text to a time using a provided format specifier.
|
||||
|
||||
Arguments:
|
||||
- text: The text to parse as a time of day, using the specified pattern.
|
||||
- pattern: The pattern to use for parsing the input text.
|
||||
- locale: The locale in which the pattern should be interpreted.
|
||||
|
||||
Returns a `Time_Error` if the provided text cannot be parsed using the
|
||||
provided pattern and locale.
|
||||
|
||||
? Pattern Syntax
|
||||
For the list of accepted symbols in pattern refer to `Time.format` doc.
|
||||
|
||||
> Example
|
||||
Parse "2020-05-06 04:30:20" as Time
|
||||
Date.parse_format "2020-05-06 04:30:20" "yyyy-MM-dd HH:mm:ss"
|
||||
|
||||
> Example
|
||||
Parse "06 of May 2020 at 04:30AM" as Time
|
||||
Date.parse_format "06 of May 2020 at 04:30AM" "dd 'of' MMMM yyyy 'at' hh:mma"
|
||||
parse_format : Text -> Text -> Locale -> Time ! Time_Error
|
||||
parse_format text pattern locale=Locale.default =
|
||||
Panic.recover (Time (Time_Utils.parse_time_format text pattern locale.java_locale)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Obtains the current date-time from the system clock in the system timezone.
|
||||
|
||||
> Example
|
||||
Get the current time
|
||||
Time.now
|
||||
now : Time
|
||||
now = Time ZonedDateTime.now
|
||||
|
||||
## Obtains an instance of `Time` from a year, month, day, hour, minute,
|
||||
second, nanosecond and timezone.
|
||||
|
||||
Arguments:
|
||||
- month: the month-of-year to represent, from 1 (January) to 12 (December)
|
||||
- day: the day-of-month to represent, from 1 to 31 and must be valid for the
|
||||
year and month
|
||||
- hour: the hour-of-day to represent, from 0 to 23
|
||||
- minute: the minute-of-hour to represent, from 0 to 59
|
||||
- second: the second-of-minute to represent, from 0 to 59
|
||||
- nanosecond: the nano-of-second to represent, from 0 to 999,999,999
|
||||
- zone: the timezone
|
||||
|
||||
Returns a `Time_Error` if the provided time cannot be represented.
|
||||
|
||||
> Example
|
||||
Create a new zoned date time at Unix epoch.
|
||||
Time.new 1970 (zone = Zone.utc)
|
||||
|
||||
> Example
|
||||
Get the 5 August 1986 at midnight.
|
||||
Time.new 1986 8 5
|
||||
new : Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Zone -> Time ! Time_Error
|
||||
new year (month = 1) (day = 1) (hour = 0) (minute = 0) (second = 0) (nanosecond = 0) (zone = Zone.system) =
|
||||
Panic.recover (Time (ZonedDateTime.of year month day hour minute second nanosecond zone.internal_zone_id)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time_Error err.getMessage)
|
||||
x -> x
|
@ -1,6 +1,6 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
import Standard.Base.Data.Time.Zone
|
||||
@ -10,9 +10,131 @@ polyglot java import java.time.Instant
|
||||
polyglot java import java.time.LocalDate
|
||||
polyglot java import org.enso.base.Time_Utils
|
||||
|
||||
## Obtains the current date from the system clock in the system timezone.
|
||||
|
||||
> Example
|
||||
Get the current date.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_now = Date.now
|
||||
now : Date
|
||||
now = Date LocalDate.now
|
||||
|
||||
## Alias for `now`.
|
||||
|
||||
> Example
|
||||
Get the current date.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_today = Date.today
|
||||
today : Date
|
||||
today = here.now
|
||||
|
||||
## Constructs a new Date from a year, month, and day.
|
||||
|
||||
Arguments
|
||||
- month: The month-of-year to represent, from 1 (January) to 12 (December).
|
||||
- day: The day-of-month to represent, from 1 to 31. It must be valid for the
|
||||
year and month.
|
||||
|
||||
Returns a `Time_Error` if the provided time is not valid.
|
||||
|
||||
> Example
|
||||
Create a new local date at Unix epoch.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_new = Date.new 1970
|
||||
|
||||
> Example
|
||||
Get the local date of 5th August 1986.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_new = Date.new 1986 8 5
|
||||
|
||||
new : Integer -> Integer -> Integer -> Date ! Time.Time_Error
|
||||
new year (month = 1) (day = 1) =
|
||||
Panic.recover (Date (LocalDate.of year month day)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Converts text containing a date into a Date object.
|
||||
|
||||
Arguments:
|
||||
- text: The text to try and parse as a date.
|
||||
- pattern: An optional pattern describing how to parse the text.
|
||||
|
||||
Returns a `Time_Error` if the provided `text` cannot be parsed using the
|
||||
provided `pattern`.
|
||||
|
||||
? Pattern Syntax
|
||||
Patterns are based on a simple sequence of letters and symbols. For
|
||||
example, "d MMM yyyy" will format "2011-12-03" as "3 Dec 2011".
|
||||
|
||||
? Default Date Formatting
|
||||
Unless you provide a custom format, the text must represent a valid date
|
||||
that can be parsed using the ISO-8601 extended local date format. The
|
||||
format consists of:
|
||||
|
||||
- Four digits or more for the year. Years in the range 0000 to 9999
|
||||
will be pre-padded by zero to ensure four digits. Years outside
|
||||
that range will have a prefixed positive or negative symbol.
|
||||
- A dash
|
||||
- Two digits for the month-of-year. This is pre-padded by zero to ensure
|
||||
two digits.
|
||||
- A dash
|
||||
- Two digits for the day-of-month. This is pre-padded by zero to ensure two
|
||||
digits.
|
||||
|
||||
> Example
|
||||
Parse the date of 23rd December 2020.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_parse = Date.parse "2020-12-23"
|
||||
|
||||
> Example
|
||||
Recover from an error due to a wrong format.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_parse_err = Date.parse "my birthday" . catch e-> case e of
|
||||
Time.Time_Error _ -> Date.new 2000 1 1
|
||||
|
||||
> Example
|
||||
Parse "1999-1-1" as Date using a custom format.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_parse = Date.parse "1999-1-1" "yyyy-M-d"
|
||||
|
||||
> Example
|
||||
Recover from the parse error.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.base.Data.Time
|
||||
|
||||
example_parse_err =
|
||||
date = Date.parse "1999-1-1" "yyyy-MM-dd"
|
||||
date.catch e-> case e of
|
||||
Time.Time_Error _ -> Date.new 2000 1 1
|
||||
parse : Text -> (Text | Nothing) -> Date ! Time.Time_Error
|
||||
parse text pattern=Nothing =
|
||||
result = Panic.recover <| case pattern of
|
||||
Nothing -> LocalDate.parse text
|
||||
Text -> LocalDate.parse text (DateTimeFormatter.ofPattern pattern)
|
||||
_ -> Panic.throw (Time.Time_Error "An invalid pattern was provided.")
|
||||
Date result . map_error <| case _ of
|
||||
Polyglot_Error err -> Time.Time_Error err.getMessage
|
||||
x -> x
|
||||
|
||||
type Date
|
||||
|
||||
## This type represents a date, often viewed as year-month-day.
|
||||
## This type represents a date, often viewed as year-month-day.
|
||||
|
||||
Arguments:
|
||||
- internal_local_date: The internal date representation.
|
||||
@ -29,7 +151,10 @@ type Date
|
||||
|
||||
> Example
|
||||
Get the current year.
|
||||
Date.now.year
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_year = Date.now.year
|
||||
year : Integer
|
||||
year = this . internal_local_date . getYear
|
||||
|
||||
@ -37,7 +162,10 @@ type Date
|
||||
|
||||
> Example
|
||||
Get the current month.
|
||||
Date.now.month
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_month = Date.now.month
|
||||
month : Integer
|
||||
month = this . internal_local_date . getMonthValue
|
||||
|
||||
@ -45,7 +173,10 @@ type Date
|
||||
|
||||
> Example
|
||||
Get the current day.
|
||||
Date.now.day
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_day = Date.now.day
|
||||
day : Integer
|
||||
day = this . internal_local_date . getDayOfMonth
|
||||
|
||||
@ -57,7 +188,12 @@ type Date
|
||||
|
||||
> Example
|
||||
Convert this date to midnight UTC time.
|
||||
Day.new 2020 2 3 . to_time Time_Of_Day.new Zone.utc
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_to_time = Date.new 2020 2 3 . to_time Time_Of_Day.new Zone.utc
|
||||
to_time : Time_Of_Day -> Zone -> Time
|
||||
to_time time_of_day (zone = Zone.system) = Time.time (this . internal_local_date . atTime time_of_day.internal_local_time . atZone zone.internal_zone_id)
|
||||
|
||||
@ -68,7 +204,11 @@ type Date
|
||||
|
||||
> Example
|
||||
Add 6 months to a local date.
|
||||
Date.new 2020 + 6.months
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_add = Date.new 2020 + 6.months
|
||||
+ : Duration -> Date
|
||||
+ amount = if amount.is_time then Error.throw (Time.Time_Error "Date does not support time intervals") else
|
||||
Date (this . internal_local_date . plus amount.internal_period)
|
||||
@ -81,7 +221,11 @@ type Date
|
||||
|
||||
> Example
|
||||
Subtract 7 days from a local date.
|
||||
Date.new 2020 - 7.days
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_subtract = Date.new 2020 - 7.days
|
||||
- : Duration -> Date
|
||||
- amount = if amount.is_time then Error.throw (Time.Time_Error "Date does not support time intervals") else
|
||||
Date (this . internal_local_date . minus amount.internal_period)
|
||||
@ -90,7 +234,10 @@ type Date
|
||||
|
||||
> Example
|
||||
Convert the current date to text.
|
||||
Date.now.to_text
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_to_text = Date.now.to_text
|
||||
to_text : Text
|
||||
to_text = Time_Utils.default_date_formatter . format this.internal_local_date
|
||||
|
||||
@ -98,7 +245,10 @@ type Date
|
||||
|
||||
> Example
|
||||
Convert the current date to JSON.
|
||||
Date.now.to_json
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_to_json = Date.now.to_json
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Date"], ["day", this.day], ["month", this.month], ["year", this.year]]
|
||||
|
||||
@ -111,130 +261,40 @@ type Date
|
||||
Patterns are based on a simple sequence of letters and symbols. For
|
||||
example, "d MMM yyyy" will format "2011-12-03" as "3 Dec 2011".
|
||||
|
||||
For the list of accepted symbols in pattern refer to the
|
||||
`Base.Data.Time.Time.format` doc.
|
||||
|
||||
> Example
|
||||
Format "2020-06-02" as "2 June 2020"
|
||||
Date.new 2020 6 2 . format "d MMMM yyyy"
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_format = Date.new 2020 6 2 . format "d MMMM yyyy"
|
||||
|
||||
> Example
|
||||
Format "2020-06-02" as "2 June 20"
|
||||
Date.new 2020 6 2 . format "d MMMM yy"
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_format = Date.new 2020 6 2 . format "d MMMM yy"
|
||||
|
||||
> Example
|
||||
Format "2020-06-02" as "Tuesday, 02 June 2020"
|
||||
Date.new 2020 6 2 . format "EEEE, dd MMMM yyyy"
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_format = Date.new 2020 6 2 . format "EEEE, dd MMMM yyyy"
|
||||
|
||||
> Example
|
||||
Format "2020-06-02" as "Tue Jun 2"
|
||||
Date.new 2020 6 2 . format "EEE MMM d"
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_format = Date.new 2020 6 2 . format "EEE MMM d"
|
||||
|
||||
> Example
|
||||
Format "2020-06-02" as "2020AD"
|
||||
Date.new 2020 6 2 . format "yyyyGG"
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
|
||||
example_format = Date.new 2020 6 2 . format "yyyyGG"
|
||||
format : Text -> Text
|
||||
format pattern = DateTimeFormatter.ofPattern pattern . format this.internal_local_date
|
||||
|
||||
## Converts text containing a date into a Date object.
|
||||
|
||||
Arguments:
|
||||
- text: The text to try and parse as a date.
|
||||
|
||||
Returns a `Time_Error` if the provided `text` cannot be parsed using the
|
||||
provided `pattern`.
|
||||
|
||||
? Date Formatting
|
||||
The text must represent a valid date and is parsed using the ISO-8601
|
||||
extended local date format. The format consists of:
|
||||
|
||||
- Four digits or more for the year. Years in the range 0000 to 9999
|
||||
will be pre-padded by zero to ensure four digits. Years outside
|
||||
that range will have a prefixed positive or negative symbol.
|
||||
- A dash
|
||||
- Two digits for the month-of-year. This is pre-padded by zero to ensure
|
||||
two digits.
|
||||
- A dash
|
||||
- Two digits for the day-of-month. This is pre-padded by zero to ensure two
|
||||
digits.
|
||||
|
||||
> Example
|
||||
Parse the date of 23rd December 2020.
|
||||
Date.parse "2020-12-23"
|
||||
|
||||
> Example
|
||||
Recover from an error due to a wrong format.
|
||||
Date.parse "my birthday" . catch e-> case e of
|
||||
Time.Error _ -> Date.new 2000 1 1
|
||||
parse : Text -> Date ! Time.Time_Error
|
||||
parse text =
|
||||
Panic.recover (Date (LocalDate.parse text)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Converts text containing a date into a Date object using a custom format.
|
||||
|
||||
Arguments:
|
||||
- text: The textual content to parse as a time.
|
||||
- pattern: The pattern describing how to parse the text.
|
||||
|
||||
Returns a `Time_Error` if the provided `text` cannot be parsed using the
|
||||
provided `pattern`.
|
||||
|
||||
? Pattern Syntax
|
||||
For the list of accepted symbols in pattern refer to the
|
||||
`Base.Data.Time.Time.format` doc.
|
||||
|
||||
> Example
|
||||
Parse "1999-1-1" as Date.
|
||||
Date.parse_format "1999-1-1" "yyyy-M-d"
|
||||
|
||||
> Example
|
||||
Recover from the parse error.
|
||||
date = Date.parse "1999-1-1" "yyyy-MM-dd"
|
||||
date.catch e-> case e of
|
||||
Time.Error msg -> Date.new 2000 1 1
|
||||
parse_format : Text -> Text -> Date ! Time.Time_Error
|
||||
parse_format text pattern =
|
||||
format = DateTimeFormatter.ofPattern pattern
|
||||
Panic.recover (Date (LocalDate.parse text format)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Obtains the current date from the system clock in the system timezone.
|
||||
|
||||
> Example
|
||||
Get the current date.
|
||||
Date.now
|
||||
now : Date
|
||||
now = Date LocalDate.now
|
||||
|
||||
## Alias for `now`.
|
||||
|
||||
> Example
|
||||
Get the current date.
|
||||
Date.today
|
||||
today : Date
|
||||
today = here.now
|
||||
|
||||
## Constructs a new Date from a year, month, and day.
|
||||
|
||||
Arguments
|
||||
- month: The month-of-year to represent, from 1 (January) to 12 (December).
|
||||
- day: The day-of-month to represent, from 1 to 31. It must be valid for the
|
||||
year and month.
|
||||
|
||||
Returns a `Time_Error` if the provided time is not valid.
|
||||
|
||||
> Example
|
||||
Create a new local date at Unix epoch.
|
||||
Date.new 1970
|
||||
|
||||
> Example
|
||||
Get the local date of 5th August 1986.
|
||||
Date.new 1986 8 5
|
||||
new : Integer -> Integer -> Integer -> Date ! Time.Time_Error
|
||||
new year (month = 1) (day = 1) =
|
||||
Panic.recover (Date (LocalDate.of year month day)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
@ -1,10 +1,27 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
polyglot java import java.time.Duration as Java_Duration
|
||||
polyglot java import java.time.Period as Java_Period
|
||||
|
||||
## Create an interval representing the duration between two points in time.
|
||||
|
||||
Arguments:
|
||||
- start_inclusive: The start time of the duration.
|
||||
- end_inclusife: The end time of the duration.
|
||||
|
||||
> Example
|
||||
An hour interval between two points in time.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
example_between = Duration.between Time.now (Time.new 2010 10 20)
|
||||
between : Time -> Time -> Duration
|
||||
between start_inclusive end_exclusive =
|
||||
Duration (Java_Period.ofDays 0 . normalized) (Java_Duration.between start_inclusive.internal_zoned_date_time end_exclusive.internal_zoned_date_time)
|
||||
|
||||
type Duration
|
||||
|
||||
## An amount of time in terms of years, months, days, hours, minutes,
|
||||
@ -23,11 +40,17 @@ type Duration
|
||||
|
||||
> Example
|
||||
Add 6 seconds to a duration of 3 minutes
|
||||
3.minutes + 6.seconds
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_add = 3.minutes + 6.seconds
|
||||
|
||||
> Example
|
||||
Add 12 hours to a duration of a month.
|
||||
1.month + 12.hours
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_add = 1.month + 12.hours
|
||||
+ : Duration -> Duration
|
||||
+ that = Duration (this.internal_period . plus that.internal_period . normalized) (this.internal_duration . plus that.internal_duration)
|
||||
|
||||
@ -38,11 +61,17 @@ type Duration
|
||||
|
||||
> Example
|
||||
Subtract 11 months from a duration of 3 years
|
||||
3.years - 11.months
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_subtract = 3.years - 11.months
|
||||
|
||||
> Example
|
||||
Substract 30 minutes from a duration of 7 months.
|
||||
7.months - 30.minutes
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_subtract = 7.months - 30.minutes
|
||||
- : Duration -> Duration
|
||||
- that = Duration (this.internal_period . minus that.internal_period . normalized) (this.internal_duration . minus that.internal_duration)
|
||||
|
||||
@ -50,7 +79,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in nanoseconds.
|
||||
1.nanosecond.nanoseconds
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_nanos = Examples.duration.nanoseconds
|
||||
nanoseconds : Integer
|
||||
nanoseconds = this.internal_duration . toNanosPart
|
||||
|
||||
@ -58,7 +90,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in milliseconds.
|
||||
1.millisecond.milliseconds
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_millis = Examples.duration.milliseconds
|
||||
milliseconds : Integer
|
||||
milliseconds = this.internal_duration . toMillisPart
|
||||
|
||||
@ -66,7 +101,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in seconds.
|
||||
1.second.seconds
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_seconds = Examples.duration.milliseconds
|
||||
seconds : Integer
|
||||
seconds = this.internal_duration . toSecondsPart
|
||||
|
||||
@ -74,7 +112,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in minutes.
|
||||
1.minute.minutes
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_minutes = Examples.duration.milliseconds
|
||||
minutes : Integer
|
||||
minutes = this.internal_duration . toMinutesPart
|
||||
|
||||
@ -82,7 +123,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in hours.
|
||||
1.hour.hours
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_hours = Examples.duration.milliseconds
|
||||
hours : Integer
|
||||
hours = this.internal_duration . toHours
|
||||
|
||||
@ -90,7 +134,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in days.
|
||||
1.day.days
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_days = Examples.duration.milliseconds
|
||||
days : Integer
|
||||
days = this.internal_period . getDays
|
||||
|
||||
@ -98,7 +145,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in months.
|
||||
1.month.months
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_months = Examples.duration.months
|
||||
months : Integer
|
||||
months = this.internal_period . getMonths
|
||||
|
||||
@ -106,7 +156,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Get the portion of the duration expressed in years.
|
||||
1.year.years
|
||||
|
||||
import Standard.Examples
|
||||
|
||||
example_years = Examples.duration.years
|
||||
years : Integer
|
||||
years = this.internal_period . getYears
|
||||
|
||||
@ -115,19 +168,27 @@ type Duration
|
||||
|
||||
> Example
|
||||
Convert duration of a year and a hour to a vector returning `[1, 0, 0, 1, 0, 0, 0]`.
|
||||
1.year . plus 1.hour . to_vector
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_to_vec = (1.year + 1.hour).to_vector
|
||||
|
||||
> Example
|
||||
Convert duration of 800 nanoseconds to a vector returning `[0, 0, 0, 0, 0, 0, 800]`
|
||||
800.nanoseconds . to_vector
|
||||
to_vector : Vector.Vector
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_to_vec = 800.nanoseconds . to_vector
|
||||
to_vector : Vector.Vector Integer
|
||||
to_vector = [this.years, this.months, this.days, this.hours, this.minutes, this.seconds, this.nanoseconds]
|
||||
|
||||
## A Duration to Json conversion.
|
||||
|
||||
> Example
|
||||
Convert a duration of 10 seconds to Json.
|
||||
10.seconds.to_json
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_to_json = 10.seconds.to_json
|
||||
to_json : Json.Object
|
||||
to_json =
|
||||
b = Vector.new_builder
|
||||
@ -145,7 +206,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Check if the duration of 10 seconds is date-based.
|
||||
10.seconds.is_date
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_is_date = 10.seconds.is_date
|
||||
is_date : Boolean
|
||||
is_date = (this.years==0 . not) || (this.months==0 . not) || (this.days==0 . not)
|
||||
|
||||
@ -153,7 +217,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Check if the duration of 10 seconds is time-based.
|
||||
10.seconds.is_time
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_is_time = 10.seconds.is_time
|
||||
is_time : Boolean
|
||||
is_time = (this.hours==0 . not) || (this.minutes==0 . not) || (this.seconds==0 . not) || (this.nanoseconds==0 . not)
|
||||
|
||||
@ -161,7 +228,10 @@ type Duration
|
||||
|
||||
> Example
|
||||
Check if the duration of 10 seconds is empty.
|
||||
10.seconds.is_empty
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_is_empty = 10.seconds.is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = this.is_date.not && this.is_time.not
|
||||
|
||||
@ -172,84 +242,186 @@ type Duration
|
||||
|
||||
> Examples
|
||||
Check if 60 seconds and 1 minute are equal.
|
||||
60.seconds == 1.minute
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_eq = 60.seconds == 1.minute
|
||||
== : Duration -> Boolean
|
||||
== that = this.to_vector == that.to_vector
|
||||
|
||||
## Duration in nanoseconds.
|
||||
## Create a duration of `this` nanoseconds.
|
||||
|
||||
> Examples
|
||||
Create a duration of 1 nanosecond.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_nano = 1.nanosecond
|
||||
Integer.nanosecond : Duration
|
||||
Integer.nanosecond = Duration (Java_Period.ofDays 0) (Java_Duration.ofNanos this)
|
||||
|
||||
## Duration in nanoseconds.
|
||||
## Create a duration of `this` nanoseconds.
|
||||
|
||||
> Examples
|
||||
Create a duration of 20 nanoseconds.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_nano = 20.nanoseconds
|
||||
Integer.nanoseconds : Duration
|
||||
Integer.nanoseconds = this.nanosecond
|
||||
|
||||
## Duration in milliseconds.
|
||||
## Create a duration of `this` milliseconds.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 millisecond.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_milli = 1.millisecond
|
||||
Integer.millisecond : Duration
|
||||
Integer.millisecond = Duration (Java_Period.ofDays 0) (Java_Duration.ofMillis this)
|
||||
|
||||
## Duration in milliseconds.
|
||||
## Create a duration of `this` milliseconds.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 milliseconds.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_milli = 20.milliseconds
|
||||
Integer.milliseconds : Duration
|
||||
Integer.milliseconds = this.millisecond
|
||||
|
||||
## Duration in seconds.
|
||||
## Create a duration of `this` seconds.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 second.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_second = 1.second
|
||||
Integer.second : Duration
|
||||
Integer.second = Duration (Java_Period.ofDays 0) (Java_Duration.ofSeconds this)
|
||||
|
||||
## Duration in seconds.
|
||||
## Create a duration of `this` seconds.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 seconds.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_second = 20.seconds
|
||||
Integer.seconds : Duration
|
||||
Integer.seconds = this.second
|
||||
|
||||
## Duration in minutes.
|
||||
## Create a duration of `this` minutes.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 minute.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_min = 1.minute
|
||||
Integer.minute : Duration
|
||||
Integer.minute = Duration (Java_Period.ofDays 0) (Java_Duration.ofMinutes this)
|
||||
|
||||
## Duration in minutes.
|
||||
## Create a duration of `this` minutes.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 minutes.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_min = 20.minutes
|
||||
Integer.minutes : Duration
|
||||
Integer.minutes = this.minute
|
||||
|
||||
## Duration in hours.
|
||||
## Create a duration of `this` hours.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 hour.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_hour = 1.hour
|
||||
Integer.hour : Duration
|
||||
Integer.hour = Duration (Java_Period.ofDays 0) (Java_Duration.ofHours this)
|
||||
|
||||
## Duration in hours.
|
||||
## Create a duration of `this` hours.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 hours.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_hour = 20.hours
|
||||
Integer.hours : Duration
|
||||
Integer.hours = this.hour
|
||||
|
||||
## Duration in days.
|
||||
## Create a duration of `this` days.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 day.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_day = 1.day
|
||||
Integer.day : Duration
|
||||
Integer.day = Duration (Java_Period.ofDays this . normalized) (Java_Duration.ofSeconds 0)
|
||||
|
||||
## Duration in days.
|
||||
## Create a duration of `this` days.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 days.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_day = 20.days
|
||||
Integer.days : Duration
|
||||
Integer.days = this.day
|
||||
|
||||
## Duration in months.
|
||||
## Create a duration of `this` months.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 month.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_month = 1.month
|
||||
Integer.month : Duration
|
||||
Integer.month = Duration (Java_Period.ofMonths this . normalized) (Java_Duration.ofSeconds 0)
|
||||
|
||||
## Duration in months.
|
||||
## Create a duration of `this` months.
|
||||
|
||||
> Example
|
||||
Create a duration of 6 months.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_month = 6.months
|
||||
Integer.months : Duration
|
||||
Integer.months = this.month
|
||||
|
||||
## Duration in years.
|
||||
## Create a duration of `this` years.
|
||||
|
||||
> Example
|
||||
Create a duration of 1 year.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_year = 1.year
|
||||
Integer.year : Duration
|
||||
Integer.year = Duration (Java_Period.ofYears this . normalized) (Java_Duration.ofSeconds 0)
|
||||
|
||||
## Duration in years.
|
||||
## Create a duration of `this` years.
|
||||
|
||||
> Example
|
||||
Create a duration of 20 years.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
|
||||
example_year = 20.years
|
||||
Integer.years : Duration
|
||||
Integer.years = this.year
|
||||
|
||||
## Create an interval representing the duration between two points in time.
|
||||
|
||||
Arguments:
|
||||
- start_inclusive: The start time of the duration.
|
||||
- end_inclusife: The end time of the duration.
|
||||
|
||||
> Example
|
||||
An hour interval between two points in time.
|
||||
Duration.between Time.now (Time.new 2010 10 20)
|
||||
between : Time -> Time -> Duration
|
||||
between start_inclusive end_exclusive =
|
||||
Duration (Java_Period.ofDays 0 . normalized) (Java_Duration.between start_inclusive.internal_zoned_date_time end_exclusive.internal_zoned_date_time)
|
||||
|
||||
|
@ -1,9 +1,9 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Locale
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Locale
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
polyglot java import java.time.format.DateTimeFormatter
|
||||
@ -11,152 +11,61 @@ polyglot java import java.time.Instant
|
||||
polyglot java import java.time.LocalTime
|
||||
polyglot java import org.enso.base.Time_Utils
|
||||
|
||||
type Time_Of_Day
|
||||
## Obtains the current time from the system clock in the default time-zone.
|
||||
|
||||
## This type is a date-time object that represents a time, often viewed
|
||||
as hour-minute-second.
|
||||
> Example
|
||||
Get the current time in the default time zone.
|
||||
|
||||
Arguments:
|
||||
- internal_local_time: The internal representation of the time of day.
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
Time is represented to nanosecond precision. For example, the value
|
||||
"13:45.30.123456789" can be stored in a `Time_Of_Day`.
|
||||
type Time_Of_Day internal_local_time
|
||||
example_now = Time_Of_Day.now
|
||||
now : Time_Of_Day
|
||||
now = Time_Of_Day LocalTime.now
|
||||
|
||||
## Get the hour portion of the time of day.
|
||||
## Obtains an instance of `Time_Of_Day` from an hour, minute, second
|
||||
and nanosecond.
|
||||
|
||||
> Example
|
||||
Get the current hour.
|
||||
Time_Of_Day.now.hour
|
||||
hour : Integer
|
||||
hour = this . internal_local_time . getHour
|
||||
Arguments:
|
||||
- hour: The hour-of-day to represent, from 0 to 23.
|
||||
- minute: The minute-of-hour to represent, from 0 to 59.
|
||||
- second: The second-of-minute to represent, from 0 to 59.
|
||||
- nanosecond: The nano-of-second to represent, from 0 to 999,999,999.
|
||||
|
||||
## Get the minute portion of the time of day.
|
||||
Returns a `Time_Error` if the provided time is not a valid time.
|
||||
|
||||
> Example
|
||||
Get the current minute.
|
||||
Time_Of_Day.now.minute
|
||||
minute : Integer
|
||||
minute = this . internal_local_time . getMinute
|
||||
> Example
|
||||
Create a new local time at Unix epoch.
|
||||
|
||||
## Get the second portion of the time of day.
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
> Example
|
||||
Get the current second.
|
||||
Time_Of_Day.now.second
|
||||
second : Integer
|
||||
second = this . internal_local_time . getSecond
|
||||
example_epoch = Time_Of_Day.new
|
||||
|
||||
## Get the nanosecond portion of the time of day.
|
||||
> Example
|
||||
Get the local time at 9:30.
|
||||
|
||||
> Example
|
||||
Get the current nanosecond.
|
||||
Time_Of_Day.now.nanosecond
|
||||
nanosecond : Integer
|
||||
nanosecond = this . internal_local_time . getNano
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
## Extracts the time as the number of seconds, from 0 to 24 * 60 * 60 - 1.
|
||||
|
||||
> Example
|
||||
Convert the current time to seconds of the day.
|
||||
Time_Of_Day.now.to_seconds
|
||||
to_seconds : Integer
|
||||
to_seconds = this . internal_local_time . toSecondOfDay
|
||||
|
||||
## Combine this time of day with a date to create a point in time.
|
||||
|
||||
Arguments:
|
||||
- date: The date on which this time should occur.
|
||||
- zone: The time-zone in which the time is specified.
|
||||
|
||||
> Example
|
||||
Convert local time to 1st January 2020 12:30 at system timezone.
|
||||
Time_Of_Day.new 12 30 . to_time (Date.new 2020)
|
||||
to_time : Date -> Zone -> Time
|
||||
to_time date (zone = Zone.system) = Time.time (this . internal_local_time . atDate date.internal_local_date . atZone zone.internal_zone_id)
|
||||
|
||||
## Add the specified amount of time to this instant to get a new instant.
|
||||
|
||||
Arguments:
|
||||
- amount: The amount of time to add to this instant.
|
||||
|
||||
> Example
|
||||
Add 3 seconds to a local time.
|
||||
Time_Of_Day.new + 3.seconds
|
||||
+ : Duration -> Time_Of_Day
|
||||
+ amount = if amount.is_date then Error.throw (Time.Time_Error "Time_Of_Day does not support date intervals") else Time_Of_Day (this . internal_local_time . plus amount.internal_duration)
|
||||
|
||||
## Subtract the specified amount of time from this instant to get a new
|
||||
instant.
|
||||
|
||||
Arguments:
|
||||
- amount: The amount of time to subtract from this instant.
|
||||
|
||||
> Example
|
||||
Subtract 12 hours from a local time.
|
||||
Time_Of_Day.new - 12.hours
|
||||
- : Duration -> Time_Of_Day
|
||||
- amount = if amount.is_date then Error.throw (Time.Time_Error "Time_Of_Day does not support date intervals") else Time_Of_Day (this . internal_local_time . minus amount.internal_duration)
|
||||
|
||||
## Format this time of day as text using the default formatter.
|
||||
|
||||
> Example
|
||||
Convert the current time to text.
|
||||
Time_Of_Day.now.to_text
|
||||
to_text : Text
|
||||
to_text = Time_Utils.default_time_of_day_formatter . format this.internal_local_time
|
||||
|
||||
## A Time_Of_Day to Json conversion.
|
||||
|
||||
> Example
|
||||
Convert the current time to JSON.
|
||||
Time_Of_Day.now.to_text
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Time_Of_Day"], ["hour", this.hour], ["minute", this.minute], ["second", this.second], ["nanosecond", this.nanosecond]]
|
||||
|
||||
## Format this time of day using the provided formatter pattern.
|
||||
|
||||
Arguments:
|
||||
- pattern: The pattern specifying how to format the time of day.
|
||||
|
||||
? Pattern Syntax
|
||||
Patterns are based on a simple sequence of letters and symbols. For
|
||||
example, "HH-mm-ss.SSS" will format "16:21:10" as "16-21-10.323".
|
||||
|
||||
For the list of accepted symbols in pattern refer to the
|
||||
`Base.Data.Time.Time.format` doc.
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "16:21:00.1234"
|
||||
Time_Of_Day.new 16 21 10 . format "HH:mm:ss.SSSS"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "16:21:00.123456789"
|
||||
Time_Of_Day.new 16 21 10 . format "HH:mm:ss.n"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "4:21pm"
|
||||
Time_Of_Day.new 16 21 10 . format "h:mma"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "04:21:10pm"
|
||||
Time_Of_Day.new 16 21 10 . format "hh:mm:ssa"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "hour:4"
|
||||
Time_Of_Day.new 16 21 10 . format "'hour:'h"
|
||||
format : Text -> Text
|
||||
format pattern = DateTimeFormatter.ofPattern pattern . format this.internal_local_time
|
||||
example_epoch = Time_Of_Day.new hour=9 minute=30
|
||||
new : Integer -> Integer -> Integer -> Integer -> Time_Of_Day ! Time.Time_Error
|
||||
new (hour = 0) (minute = 0) (second = 0) (nanosecond = 0) =
|
||||
Panic.recover (Time_Of_Day (LocalTime.of hour minute second nanosecond)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Obtains an instance of `Time_Of_Day` from a text such as "10:15".
|
||||
|
||||
Arguments:
|
||||
- text: The text to parse as a time of day.
|
||||
- pattern: The pattern to use for parsing the input text.
|
||||
- locale: The locale in which the pattern should be interpreted.
|
||||
|
||||
Returns a `Time_Error` if the provided text cannot be parsed using the
|
||||
default format.
|
||||
|
||||
? Valid Time Format
|
||||
? Pattern Syntax
|
||||
For the list of accepted symbols in pattern refer to `Time.format` doc.
|
||||
|
||||
? Default Time Format
|
||||
The text must represent a valid time and is parsed using the ISO-8601
|
||||
extended local time format. The format consists of:
|
||||
|
||||
@ -177,74 +86,232 @@ type Time_Of_Day
|
||||
|
||||
> Example
|
||||
Get the time 15:05:30.
|
||||
Time_Of_Day.parse "15:05:30"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_parse = Time_Of_Day.parse "15:05:30"
|
||||
|
||||
> Example
|
||||
Recover from the parse error.
|
||||
Time_Of_Day.parse "half past twelve" . catch e-> case e of
|
||||
Time.Error _ -> Time_Of_Day.new
|
||||
parse : Text -> Time_Of_Day ! Time.Time_Error
|
||||
parse text =
|
||||
Panic.recover (Time_Of_Day (LocalTime.parse text)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
||||
## Obtains an instance of Time_Of_Day from a text using custom format.
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
Arguments:
|
||||
- text: The text to parse as a time of day, using the specified pattern.
|
||||
- pattern: The pattern to use for parsing the input text.
|
||||
- locale: The locale in which the pattern should be interpreted.
|
||||
|
||||
Returns a `Time_Error` if the provided text cannot be parsed using the
|
||||
provided pattern and locale.
|
||||
|
||||
? Pattern Syntax
|
||||
For the list of accepted symbols in pattern refer to the
|
||||
`Base.Data.Time.Time.format` doc.
|
||||
example_parse = Time_Of_Day.parse "half twelve" . catch e-> case e of
|
||||
Time.Time_Error _ -> Time_Of_Day.new
|
||||
|
||||
> Example
|
||||
Parse "04:30:20" as Time_Of_Day.
|
||||
Date.parse_format "04:30:20" "HH:mm:ss"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_parse = Time_Of_Day.parse "04:30:20" "HH:mm:ss"
|
||||
|
||||
> Example
|
||||
Parse "4:30AM" as Time_Of_Day
|
||||
Date.parse_format "4:30AM" "h:mma"
|
||||
parse_format : Text -> Text -> Locale -> Time_Of_Day ! Time.Time_Error
|
||||
parse_format text pattern locale=Locale.default =
|
||||
format = (DateTimeFormatter.ofPattern pattern).withLocale locale.java_locale
|
||||
Panic.recover (Time_Of_Day (LocalTime.parse text format)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_parse = Time_Of_Day.parse "4:30AM" "h:mma"
|
||||
parse : Text -> Text ! Nothing -> Locale -> Time_Of_Day ! Time.Time_Error
|
||||
parse text pattern=Nothing locale=Locale.default =
|
||||
result = Panic.recover <| case pattern of
|
||||
Nothing -> LocalTime.parse text
|
||||
Text ->
|
||||
formatter = DateTimeFormatter.ofPattern pattern
|
||||
LocalTime.parse text (formatter.withLocale locale.java_locale)
|
||||
_ -> Panic.throw (Time.Time_Error "An invalid pattern was provided.")
|
||||
Time_Of_Day result . map_error <| case _ of
|
||||
Polyglot_Error err -> Time.Time_Error err.getMessage
|
||||
x -> x
|
||||
|
||||
## Obtains the current time from the system clock in the default time-zone.
|
||||
type Time_Of_Day
|
||||
|
||||
> Example
|
||||
Get the current time in the default time zone.
|
||||
Time_Of_Day.now
|
||||
now : Time_Of_Day
|
||||
now = Time_Of_Day LocalTime.now
|
||||
## PRIVATE
|
||||
|
||||
## Obtains an instance of `Time_Of_Day` from an hour, minute, second
|
||||
and nanosecond.
|
||||
This type is a date-time object that represents a time, often viewed
|
||||
as hour-minute-second.
|
||||
|
||||
Arguments:
|
||||
- hour: The hour-of-day to represent, from 0 to 23.
|
||||
- minute: The minute-of-hour to represent, from 0 to 59.
|
||||
- second: The second-of-minute to represent, from 0 to 59.
|
||||
- nanosecond: The nano-of-second to represent, from 0 to 999,999,999.
|
||||
Arguments:
|
||||
- internal_local_time: The internal representation of the time of day.
|
||||
|
||||
Returns a `Time_Error` if the provided time is not a valid time.
|
||||
Time is represented to nanosecond precision. For example, the value
|
||||
"13:45.30.123456789" can be stored in a `Time_Of_Day`.
|
||||
type Time_Of_Day internal_local_time
|
||||
|
||||
> Example
|
||||
Create a new local time at Unix epoch.
|
||||
Time_Of_Day.new
|
||||
## Get the hour portion of the time of day.
|
||||
|
||||
> Example
|
||||
Get the current hour.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_hour = Time_Of_Day.now.hour
|
||||
hour : Integer
|
||||
hour = this . internal_local_time . getHour
|
||||
|
||||
## Get the minute portion of the time of day.
|
||||
|
||||
> Example
|
||||
Get the current minute.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_minute = Time_Of_Day.now.minute
|
||||
minute : Integer
|
||||
minute = this . internal_local_time . getMinute
|
||||
|
||||
## Get the second portion of the time of day.
|
||||
|
||||
> Example
|
||||
Get the current second.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_second = Time_Of_Day.now.second
|
||||
second : Integer
|
||||
second = this . internal_local_time . getSecond
|
||||
|
||||
## Get the nanosecond portion of the time of day.
|
||||
|
||||
> Example
|
||||
Get the current nanosecond.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_nanosecond = Time_Of_Day.now.nanosecond
|
||||
nanosecond : Integer
|
||||
nanosecond = this . internal_local_time . getNano
|
||||
|
||||
## Extracts the time as the number of seconds, from 0 to 24 * 60 * 60 - 1.
|
||||
|
||||
> Example
|
||||
Convert the current time into elapsed seconds in the day.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_to_seconds = Time_Of_Day.now.to_seconds
|
||||
to_seconds : Integer
|
||||
to_seconds = this . internal_local_time . toSecondOfDay
|
||||
|
||||
## Combine this time of day with a date to create a point in time.
|
||||
|
||||
Arguments:
|
||||
- date: The date on which this time should occur.
|
||||
- zone: The time-zone in which the time is specified.
|
||||
|
||||
> Example
|
||||
Convert local time to 1st January 2020 12:30 at system timezone.
|
||||
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_to_time = Time_Of_Day.new 12 30 . to_time (Date.new 2020)
|
||||
to_time : Date -> Zone -> Time
|
||||
to_time date (zone = Zone.system) =
|
||||
Time.time (this . internal_local_time . atDate date.internal_local_date . atZone zone.internal_zone_id)
|
||||
|
||||
## Add the specified amount of time to this instant to get a new instant.
|
||||
|
||||
Arguments:
|
||||
- amount: The amount of time to add to this instant.
|
||||
|
||||
> Example
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_plus = Time_Of_Day.new + 3.seconds
|
||||
+ : Duration -> Time_Of_Day
|
||||
+ amount = if amount.is_date then Error.throw (Time.Time_Error "Time_Of_Day does not support date intervals") else
|
||||
Time_Of_Day (this . internal_local_time . plus amount.internal_duration)
|
||||
|
||||
## Subtract the specified amount of time from this instant to get a new
|
||||
instant.
|
||||
|
||||
Arguments:
|
||||
- amount: The amount of time to subtract from this instant.
|
||||
|
||||
> Example
|
||||
Subtract 12 hours from a local time.
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_minus = Time_Of_Day.now - 12.hours
|
||||
- : Duration -> Time_Of_Day
|
||||
- amount = if amount.is_date then Error.throw (Time.Time_Error "Time_Of_Day does not support date intervals") else
|
||||
Time_Of_Day (this . internal_local_time . minus amount.internal_duration)
|
||||
|
||||
## Format this time of day as text using the default formatter.
|
||||
|
||||
> Example
|
||||
Convert the current time to text.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_to_text = Time_Of_Day.now.to_text
|
||||
to_text : Text
|
||||
to_text = Time_Utils.default_time_of_day_formatter . format this.internal_local_time
|
||||
|
||||
## A Time_Of_Day to Json conversion.
|
||||
|
||||
> Example
|
||||
Convert the current time to JSON.
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_to_json = Time_Of_Day.now.to_text
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Time_Of_Day"], ["hour", this.hour], ["minute", this.minute], ["second", this.second], ["nanosecond", this.nanosecond]]
|
||||
|
||||
## Format this time of day using the provided formatter pattern.
|
||||
|
||||
Arguments:
|
||||
- pattern: The pattern specifying how to format the time of day.
|
||||
|
||||
? Pattern Syntax
|
||||
Patterns are based on a simple sequence of letters and symbols. For
|
||||
example, "HH-mm-ss.SSS" will format "16:21:10" as "16-21-10.323".
|
||||
|
||||
For the list of accepted symbols in pattern refer to the
|
||||
`Base.Data.Time.format` doc.
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "16:21:00.1234"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_format = Time_Of_Day.new 16 21 10 . format "HH:mm:ss.SSSS"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "16:21:00.123456789"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_format = Time_Of_Day.new 16 21 10 . format "HH:mm:ss.n"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "4:21pm"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_format = Time_Of_Day.new 16 21 10 . format "h:mma"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "04:21:10pm"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_format = Time_Of_Day.new 16 21 10 . format "hh:mm:ssa"
|
||||
|
||||
> Example
|
||||
Format "16:21:10" as "hour:4"
|
||||
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
|
||||
example_format = Time_Of_Day.new 16 21 10 . format "'hour:'h"
|
||||
format : Text -> Text
|
||||
format pattern =
|
||||
DateTimeFormatter.ofPattern pattern . format this.internal_local_time
|
||||
|
||||
> Example
|
||||
Get the local time at 9:30.
|
||||
Time_Of_Day.new 9 30
|
||||
new : Integer -> Integer -> Integer -> Integer -> Time_Of_Day ! Time.Time_Error
|
||||
new (hour = 0) (minute = 0) (second = 0) (nanosecond = 0) =
|
||||
Panic.recover (Time_Of_Day (LocalTime.of hour minute second nanosecond)) . catch e-> case e of
|
||||
Polyglot_Error err -> Error.throw (Time.Time_Error err.getMessage)
|
||||
x -> x
|
||||
|
@ -3,62 +3,14 @@ from Standard.Base import all
|
||||
polyglot java import java.time.ZoneId
|
||||
polyglot java import java.time.ZoneOffset
|
||||
|
||||
type Zone
|
||||
|
||||
## A type representing a time zone.
|
||||
|
||||
Arguments:
|
||||
- internal_zone_id: The identifier for the internal zone of the
|
||||
representation.
|
||||
|
||||
A time zone can be eiter offset-based like "-06:00" or id-based like
|
||||
"Europe/Paris".
|
||||
type Zone internal_zone_id
|
||||
|
||||
## Get the unique timezone ID.
|
||||
|
||||
> Example
|
||||
Get the unique identifier for your system's current timezone.
|
||||
Zone.system.zone_id
|
||||
zone_id : Text
|
||||
zone_id = this.internal_zone_id . getId
|
||||
|
||||
## Convert the time zone to JSON.
|
||||
|
||||
> Example
|
||||
Convert your system's current timezone to JSON.
|
||||
Zone.system.to_json
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Zone"], ["id", this.zone_id]]
|
||||
|
||||
## This method parses the ID producing a `Zone`.
|
||||
|
||||
Arguments:
|
||||
- text: The text representing a zone identifier.
|
||||
|
||||
> Example
|
||||
Get Central European Time.
|
||||
Zone.parse "CET"
|
||||
|
||||
> Example
|
||||
Get Moscow time.
|
||||
Zone.parse "Europe/Moscow"
|
||||
|
||||
> Example
|
||||
Get time zone -06:00.
|
||||
Zone.parse "-06:00"
|
||||
|
||||
> Example
|
||||
Get custom offset +03:02:01 of 3 hours 2 minutes an 1 second.
|
||||
Zone.parse "+03:02:01"
|
||||
parse : Text -> Zone
|
||||
parse text = Zone (ZoneId.of text)
|
||||
|
||||
## The system default timezone.
|
||||
|
||||
> Example
|
||||
Get the system default timezone.
|
||||
Zone.system
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_system = Zone.system
|
||||
system : Zone
|
||||
system = Zone ZoneId.systemDefault
|
||||
|
||||
@ -66,7 +18,10 @@ system = Zone ZoneId.systemDefault
|
||||
|
||||
> Example
|
||||
Get the system's local timezone.
|
||||
Zone.local
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_local = Zone.local
|
||||
local : Zone
|
||||
local = here.system
|
||||
|
||||
@ -74,7 +29,10 @@ local = here.system
|
||||
|
||||
> Example
|
||||
Get the UTC timezone.
|
||||
Zone.utc
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_utc = Zone.utc
|
||||
utc : Zone
|
||||
utc = here.parse "UTC"
|
||||
|
||||
@ -90,7 +48,81 @@ utc = here.parse "UTC"
|
||||
|
||||
> Example
|
||||
Get time zone 1 hour 1 minute and 50 seconds from UTC.
|
||||
Zone.new 1 1 50
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_new = Zone.new 1 1 50
|
||||
new : Integer -> Integer -> Integer -> Zone
|
||||
new (hours = 0) (minutes = 0) (seconds = 0) =
|
||||
Zone (ZoneOffset.ofHoursMinutesSeconds hours minutes seconds . normalized)
|
||||
|
||||
## This method parses the ID producing a `Zone`.
|
||||
|
||||
Arguments:
|
||||
- text: The text representing a zone identifier.
|
||||
|
||||
> Example
|
||||
Get Central European Time.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_parse = Zone.parse "CET"
|
||||
|
||||
> Example
|
||||
Get Moscow time.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_parse = Zone.parse "Europe/Moscow"
|
||||
|
||||
> Example
|
||||
Get time zone -06:00.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_parse = Zone.parse "-06:00"
|
||||
|
||||
> Example
|
||||
Get custom offset +03:02:01 of 3 hours 2 minutes an 1 second.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_parse = Zone.parse "+03:02:01"
|
||||
parse : Text -> Zone
|
||||
parse text = Zone (ZoneId.of text)
|
||||
|
||||
type Zone
|
||||
|
||||
## PRIVATE
|
||||
|
||||
A type representing a time zone.
|
||||
|
||||
Arguments:
|
||||
- internal_zone_id: The identifier for the internal zone of the
|
||||
representation.
|
||||
|
||||
A time zone can be eiter offset-based like "-06:00" or id-based like
|
||||
"Europe/Paris".
|
||||
type Zone internal_zone_id
|
||||
|
||||
## Get the unique timezone ID.
|
||||
|
||||
> Example
|
||||
Get the unique identifier for your system's current timezone.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_zone_id = Zone.system.zone_id
|
||||
zone_id : Text
|
||||
zone_id = this.internal_zone_id . getId
|
||||
|
||||
## Convert the time zone to JSON.
|
||||
|
||||
> Example
|
||||
Convert your system's current timezone to JSON.
|
||||
|
||||
import Standard.Base.Data.Time.Zone
|
||||
|
||||
example_to_json = Zone.system.to_json
|
||||
to_json : Json.Object
|
||||
to_json = Json.from_pairs [["type", "Zone"], ["id", this.zone_id]]
|
||||
|
@ -13,11 +13,12 @@ from Builtins import Array
|
||||
is the recommended data structure for most applications.
|
||||
|
||||
> Example
|
||||
To create a vector containing the numbers 1 through 50:
|
||||
Create a vector containing the numbers 1 through 50.
|
||||
Vector.new 50 (ix -> ix + 1)
|
||||
|
||||
> Example
|
||||
To create a copy of the given vector (`my_vec`):
|
||||
Create a copy of the given vector (`my_vec`).
|
||||
|
||||
Vector.new my_vec.length (ix -> my_vec.at ix)
|
||||
new : Number -> (Number -> Any) -> Vector
|
||||
new length constructor =
|
||||
@ -36,8 +37,8 @@ new length constructor =
|
||||
is the recommended data structure for most applications.
|
||||
|
||||
> Example
|
||||
A vector containing 50 elements, each being the number `42`, can be
|
||||
created by:
|
||||
Create a vector containing 50 elements, each being the number `42`.
|
||||
|
||||
Vector.fill length=50 item=42
|
||||
fill : Number -> Any -> Vector
|
||||
fill length ~item =
|
||||
@ -55,25 +56,22 @@ fill length ~item =
|
||||
is the recommended data structure for most applications.
|
||||
|
||||
> Example
|
||||
In the following example we'll read items from the standard input,
|
||||
until the string "end" is entered by the user and then return a vector
|
||||
containing all items.
|
||||
from Standard.Base import all
|
||||
Construct a vector using a builder that contains the items 1 to 10.
|
||||
|
||||
main =
|
||||
example_new_builder =
|
||||
builder = Vector.new_builder
|
||||
do_read =
|
||||
item = IO.readln
|
||||
if item == "end" then Nothing else
|
||||
builder.append item
|
||||
do_read
|
||||
do_read
|
||||
vec = builder.to_vector
|
||||
IO.println vec
|
||||
do_build start stop =
|
||||
builder.append start
|
||||
if start >= stop then Nothing else
|
||||
@Tail_Call do_build start+1 stop
|
||||
do_build 1 10
|
||||
builder.to_vector
|
||||
new_builder : Builder
|
||||
new_builder = Builder.new
|
||||
|
||||
## Converts a polyglot value representing an array into a vector.
|
||||
## ADVANCED
|
||||
|
||||
Converts a polyglot value representing an array into a vector.
|
||||
|
||||
Arguments:
|
||||
- arr: The polyglot array value to wrap into a vector.
|
||||
@ -87,7 +85,7 @@ new_builder = Builder.new
|
||||
As Enso vectors implement immutable semantics, this constructor function
|
||||
makes a copy of each value in the argument array.
|
||||
|
||||
If this didn't happen then it would be possible for the underlyinf array to
|
||||
If this didn't happen then it would be possible for the underlying array to
|
||||
be mutated under the hood, and sneak mutability into our immutable data.
|
||||
from_array : Any -> Vector.Vector
|
||||
from_array arr = here.new (Polyglot.get_array_size arr) (arr.at _)
|
||||
@ -119,7 +117,8 @@ type Vector
|
||||
|
||||
> Example
|
||||
Checking the length of a vector.
|
||||
[1, 2, 3, 4].length == 4
|
||||
|
||||
[1, 2, 3, 4].length
|
||||
length : Number
|
||||
length = Polyglot.get_array_size this.to_array
|
||||
|
||||
@ -129,7 +128,8 @@ type Vector
|
||||
- index: The location in the vector to get the element from.
|
||||
|
||||
> Example
|
||||
To get the second element of the vector `[1, 2, 3]`, use:
|
||||
Get the second element of a vector.
|
||||
|
||||
[1, 2, 3].at 1
|
||||
at : Number -> Any
|
||||
at index = this.to_array.at index
|
||||
@ -147,8 +147,8 @@ type Vector
|
||||
f (...(f (f init l0) l1)...) ln
|
||||
|
||||
> Example
|
||||
In the following example, we'll compute the sum of all elements of a
|
||||
vector:
|
||||
Compute the sum of all of the elements in a vector.
|
||||
|
||||
[0, 1, 2] . fold 0 (+)
|
||||
fold : Any -> (Any -> Any -> Any) -> Any
|
||||
fold init function =
|
||||
@ -164,8 +164,8 @@ type Vector
|
||||
If the vector is empty, it throws Nothing.
|
||||
|
||||
> Example
|
||||
In the following example, we'll compute the sum of all elements of a
|
||||
vector:
|
||||
Compute the sum of all the elements in a vector.
|
||||
|
||||
[0, 1, 2] . reduce (+)
|
||||
reduce : (Any -> Any -> Any) -> Any ! Nothing
|
||||
reduce function =
|
||||
@ -179,8 +179,7 @@ type Vector
|
||||
have the `+` operator used to combine them.
|
||||
|
||||
> Example
|
||||
In the following example, we'll compute the sum of all elements of a
|
||||
vector.
|
||||
Compute the sum of all fo the elements in a vector.
|
||||
|
||||
[0, 1, 2].sum
|
||||
sum : (Any ! Nothing)
|
||||
@ -199,8 +198,9 @@ type Vector
|
||||
function.
|
||||
|
||||
> Example
|
||||
Checking if any element of the list is larger than 3.
|
||||
[1,2,3,4,5].exists (> 3)
|
||||
Check if any element of the list is larger than 3.
|
||||
|
||||
[1, 2, 3, 4, 5].exists (> 3)
|
||||
exists : (Any -> Boolean) -> Boolean
|
||||
exists predicate =
|
||||
len = this.length
|
||||
@ -218,7 +218,8 @@ type Vector
|
||||
|
||||
> Example
|
||||
Finding a first element of the list that is larger than 3.
|
||||
[1,2,3,4,5].find (> 3)
|
||||
|
||||
[1, 2, 3, 4, 5].find (> 3)
|
||||
find : (Any -> Boolean) -> Any ! Nothing
|
||||
find predicate =
|
||||
len = this.length
|
||||
@ -229,7 +230,6 @@ type Vector
|
||||
@Tail_Call go idx+1
|
||||
go 0
|
||||
|
||||
|
||||
## Checks whether a predicate holds for at least one element of this vector.
|
||||
|
||||
Arguments:
|
||||
@ -239,7 +239,8 @@ type Vector
|
||||
|
||||
> Example
|
||||
Checking if any element of the list is larger than 3.
|
||||
[1,2,3,4,5].any (> 3)
|
||||
|
||||
[1, 2, 3, 4, 5].any (> 3)
|
||||
any : (Any -> Boolean) -> Boolean
|
||||
any predicate = this.exists predicate
|
||||
|
||||
@ -252,6 +253,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
Check if all elements in the vector are less than zero.
|
||||
|
||||
[-1, 1, 5, 8].all (< 0)
|
||||
all : (Any -> Boolean) -> Boolean
|
||||
all predicate = this.fold True (l -> r -> l && predicate r)
|
||||
@ -263,6 +265,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
Checking if the vector contains the number 72.
|
||||
|
||||
[1, 383, 72, 301].contains 72
|
||||
contains : Any -> Boolean
|
||||
contains elem = this.exists ix-> ix == elem
|
||||
@ -271,8 +274,8 @@ type Vector
|
||||
|
||||
> Example
|
||||
Checking for emptiness.
|
||||
[].is_empty == True
|
||||
[1].is_empty == False
|
||||
|
||||
[].is_empty
|
||||
is_empty : Boolean
|
||||
is_empty = this.length == 0
|
||||
|
||||
@ -280,8 +283,8 @@ type Vector
|
||||
|
||||
> Example
|
||||
Checking for non-emptiness.
|
||||
[].not_empty == False
|
||||
[1].not_empty == True
|
||||
|
||||
[1].not_empty
|
||||
not_empty : Boolean
|
||||
not_empty = this.is_empty.not
|
||||
|
||||
@ -294,6 +297,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
Selecting all elements that are greater than 3.
|
||||
|
||||
[1, 2, 3, 4, 5].filter (> 3)
|
||||
filter : (Any -> Boolean) -> Vector
|
||||
filter predicate =
|
||||
@ -308,10 +312,9 @@ type Vector
|
||||
some transformation of that element.
|
||||
|
||||
> Example
|
||||
In the following example, we add `1` to each element of the vector:
|
||||
Add 1 to each element of the vector.
|
||||
|
||||
[1, 2, 3] . map +1
|
||||
The result of running the code above is:
|
||||
[2, 3, 4]
|
||||
map : (Any -> Any) -> Vector
|
||||
map function =
|
||||
here.new this.length i-> function (this.at i)
|
||||
@ -324,8 +327,8 @@ type Vector
|
||||
it, and returns a vector.
|
||||
|
||||
> Example
|
||||
In the following example, we replace each number `n` with itself
|
||||
repeated `n` times:
|
||||
Replace each number `n` in the vector with itself repeated n times.
|
||||
|
||||
[0, 1, 2] . flat_map (n -> Vector.fill n n)
|
||||
flat_map : (Any -> Vector) -> Vector
|
||||
flat_map function =
|
||||
@ -348,7 +351,8 @@ type Vector
|
||||
element itself.
|
||||
|
||||
> Example
|
||||
Summing numbers with their indices in a vector.
|
||||
Sum numbers with their indices in a vector.
|
||||
|
||||
[1, 2, 3].map_with_index (+)
|
||||
map_with_index : (Integer -> Any -> Any) -> Vector
|
||||
map_with_index function = here.new this.length i-> function i (this.at i)
|
||||
@ -362,8 +366,8 @@ type Vector
|
||||
therefore it is only useful for side-effecting computations.
|
||||
|
||||
> Example
|
||||
In the following example, we're printing each element of the vector
|
||||
to the standard output:
|
||||
Print each element in the vector to standard output.
|
||||
|
||||
[1, 2, 3, 4, 5] . each IO.println
|
||||
each : (Any -> Any) -> Nothing
|
||||
each f =
|
||||
@ -374,7 +378,8 @@ type Vector
|
||||
the opposite order.
|
||||
|
||||
> Example
|
||||
Reversing a two-element vector.
|
||||
Reverse a two-element vector.
|
||||
|
||||
[1, 2].reverse
|
||||
reverse : Vector
|
||||
reverse = here.new this.length (i -> this.at (this.length - (1 + i)))
|
||||
@ -382,7 +387,8 @@ type Vector
|
||||
## Generates a human-readable text representation of the vector.
|
||||
|
||||
> Example
|
||||
Converting a vector of numbers to text.
|
||||
Convert a vector of numbers to text.
|
||||
|
||||
[1, 2, 3].to_text == "[1, 2, 3]"
|
||||
to_text : Text
|
||||
to_text =
|
||||
@ -401,7 +407,8 @@ type Vector
|
||||
their items are pairwise equal.
|
||||
|
||||
> Example
|
||||
Comparing two vectors for equality (this case is false).
|
||||
Compare two vectors for equality (this case is false).
|
||||
|
||||
[1, 2, 3] == [2, 3, 4]
|
||||
== : Vector -> Boolean
|
||||
== that =
|
||||
@ -415,8 +422,9 @@ type Vector
|
||||
- that: The vector to concatenate to the end of `this`.
|
||||
|
||||
> Example
|
||||
Concatenating two single-element vectors.
|
||||
[1] + [2] == [1, 2]
|
||||
Concatenate two single-element vectors.
|
||||
|
||||
[1] + [2]
|
||||
+ : Vector -> Vector
|
||||
+ that =
|
||||
this_len = this.length
|
||||
@ -433,8 +441,9 @@ type Vector
|
||||
- `element`: An element to add to this vector.
|
||||
|
||||
> Example
|
||||
Add one element in front:
|
||||
[2, 3].prepend 1 == [1, 2, 3]
|
||||
Add 1 to the start of the vector.
|
||||
|
||||
[2, 3].prepend 1
|
||||
prepend : Any -> Vector
|
||||
prepend element = [element] + this
|
||||
|
||||
@ -444,8 +453,9 @@ type Vector
|
||||
- `element`: An element to add to this vector.
|
||||
|
||||
> Example
|
||||
Add one element to the end:
|
||||
[1, 2].append 3 == [1, 2, 3]
|
||||
Add 3 to the end of the vector.
|
||||
|
||||
[1, 2].append 3
|
||||
append : Any -> Vector
|
||||
append element = this + [element]
|
||||
|
||||
@ -456,7 +466,8 @@ type Vector
|
||||
- separator: The text to use to join the textual elements of the vector.
|
||||
|
||||
> Example
|
||||
The following code returns "foo, bar, baz"
|
||||
Join the elements of the vector together as a string.
|
||||
|
||||
["foo", "bar", "baz"].join ", "
|
||||
join : String -> Text
|
||||
join separator =
|
||||
@ -470,7 +481,8 @@ type Vector
|
||||
- count: The number of elements to drop from the start of `this`.
|
||||
|
||||
> Example
|
||||
The following code returns [2, 3, 4, 5]
|
||||
Remove the first element from the start of the vector.
|
||||
|
||||
[1, 2, 3, 4, 5].drop_start 1
|
||||
drop_start : Integer -> Vector
|
||||
drop_start count = if count >= this.length then here.new 0 (x -> x) else
|
||||
@ -482,7 +494,8 @@ type Vector
|
||||
- count: The number of elements to drop from the end of `this`.
|
||||
|
||||
> Example
|
||||
The following code returns [1, 2, 3]
|
||||
Remove the last two elements from the end of the vector.
|
||||
|
||||
[1, 2, 3, 4, 5].drop_end 2
|
||||
drop_end : Integer -> Vector
|
||||
drop_end count = if count >= this.length then here.new 0 (x -> x) else
|
||||
@ -495,7 +508,8 @@ type Vector
|
||||
- count: The number of elements to take from the start of `this`.
|
||||
|
||||
> Example
|
||||
The following code returns [1, 2]
|
||||
Create a new vector from the first two elements of the vector.
|
||||
|
||||
[1, 2, 3, 4, 5].take_start 2
|
||||
take_start : Integer -> Vector
|
||||
take_start count = if count >= this.length then this else
|
||||
@ -508,7 +522,8 @@ type Vector
|
||||
- count: The number of elements to take from the end of `this`.
|
||||
|
||||
> Example
|
||||
The following code returns [3, 4, 5]
|
||||
Create a new vector from the last two elements of the vector.
|
||||
|
||||
[1, 2, 3, 4, 5].take_end 3
|
||||
take_end : Integer -> Vector
|
||||
take_end count = if count >= this.length then this else
|
||||
@ -526,12 +541,14 @@ type Vector
|
||||
`this` and `that`, containing results of calling `function`.
|
||||
|
||||
> Example
|
||||
To pairwise-sum two vectors:
|
||||
[1, 2, 3].zip [4, 5, 6] (+) == [5, 7, 9]
|
||||
Calculate the pairwise sum of two vectors.
|
||||
|
||||
[1, 2, 3].zip [4, 5, 6] (+)
|
||||
|
||||
> Example
|
||||
When the `function` is not provided, it defaults to creating a pair
|
||||
of both elements:
|
||||
of both elements.
|
||||
|
||||
[1, 2, 3].zip [4, 5, 6] == [[1, 4], [2, 5], [3, 6]]
|
||||
zip : Vector -> (Any -> Any -> Any) -> Vector
|
||||
zip that function=[_,_] =
|
||||
@ -550,10 +567,12 @@ type Vector
|
||||
|
||||
> Example
|
||||
Extending vector to the length of 5 returns `[1, 2, 3, 0, 0]`
|
||||
|
||||
[1, 2, 3].pad 5 0
|
||||
|
||||
> Example
|
||||
Extending vector to the length of 5 returns `[1, 2, 3, 4, 5]`
|
||||
|
||||
[1, 2, 3, 4, 5].pad 5 0
|
||||
pad : Integer -> Any -> Vector
|
||||
pad n elem =
|
||||
@ -564,6 +583,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
Convert a vector of numbers to JSON.
|
||||
|
||||
[1, 2, 3].to_json
|
||||
to_json : Json.Array
|
||||
to_json = Json.Array (this.map .to_json)
|
||||
@ -573,6 +593,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns 1.
|
||||
|
||||
[1, 2, 3, 4].head
|
||||
head : Any ! Nothing
|
||||
head = if this.length >= 1 then this.at 0 else Error.throw Nothing
|
||||
@ -581,6 +602,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns [2, 3, 4].
|
||||
|
||||
[1, 2, 3, 4].tail
|
||||
tail : Vector ! Nothing
|
||||
tail = if this.length >= 1 then this.drop_start 1 else Error.throw Nothing
|
||||
@ -589,6 +611,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns [1, 2, 3].
|
||||
|
||||
[1, 2, 3, 4].init
|
||||
init : Vector ! Nothing
|
||||
init = if this.length >= 1 then this.drop_end 1 else Error.throw Nothing
|
||||
@ -598,6 +621,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns 4.
|
||||
|
||||
[1, 2, 3, 4].last
|
||||
last : Vector ! Nothing
|
||||
last = if this.length >= 1 then (this.take_end 1).at 0 else Error.throw Nothing
|
||||
@ -607,6 +631,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns 1.
|
||||
|
||||
[1, 2, 3, 4].first
|
||||
first : Vector ! Nothing
|
||||
first = this.head
|
||||
@ -618,6 +643,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
The following code returns 2.
|
||||
|
||||
[1, 2, 3, 4].second
|
||||
second : Vector ! Nothing
|
||||
second = if this.length >= 2 then this.at 1 else Error.throw Nothing
|
||||
@ -630,6 +656,7 @@ type Vector
|
||||
|
||||
> Example
|
||||
Empty vectors return `Nothing`.
|
||||
|
||||
[].rest == Nothing
|
||||
rest : Vector ! Nothing
|
||||
rest = this.tail
|
||||
@ -668,10 +695,12 @@ type Vector
|
||||
|
||||
> Example
|
||||
Sorting a vector of numbers.
|
||||
|
||||
[5, 2, 3, 45, 15].sort == [2, 3, 5, 15, 45]
|
||||
|
||||
> Example
|
||||
Sorting a vector of `Pair`s on the first element, descending.
|
||||
|
||||
[Pair 1 2, Pair -1 8].sort (_.first) (order = Sort_Order.Descending)
|
||||
sort : (Any -> Any) -> (Any -> Any -> Ordering) -> Sort_Order -> Vector
|
||||
sort (on = x -> x) (by = (_.compare_to _)) (order = Sort_Order.Ascending) =
|
||||
@ -702,7 +731,9 @@ type Vector
|
||||
json.to_text
|
||||
|
||||
type Builder
|
||||
## A builder type for Enso vectors.
|
||||
## PRIVATE
|
||||
|
||||
A builder type for Enso vectors.
|
||||
|
||||
Arguments:
|
||||
- to_array: The accumulator for the new vector.
|
||||
@ -716,24 +747,24 @@ type Builder
|
||||
In the following example we'll read items from the standard input,
|
||||
until the string "end" is entered by the user and then return a vector
|
||||
containing all items.
|
||||
from Standard.Base import all
|
||||
|
||||
main =
|
||||
builder = Vector.new_builder
|
||||
do_read =
|
||||
item = IO.readln
|
||||
if item == "end" then Nothing else
|
||||
builder.append item
|
||||
do_read
|
||||
do_read
|
||||
vec = builder.to_vector
|
||||
IO.println vec
|
||||
Construct a vector using a builder that contains the items 1 to 10.
|
||||
|
||||
example_new_builder =
|
||||
builder = Vector.new_builder
|
||||
do_build start stop =
|
||||
builder.append start
|
||||
if start >= stop then Nothing else
|
||||
@Tail_Call do_build start+1 stop
|
||||
do_build 1 10
|
||||
builder.to_vector
|
||||
type Builder to_array length
|
||||
|
||||
## Creates a new builder.
|
||||
|
||||
> Example
|
||||
Make a new builder
|
||||
|
||||
Vector.new_builder
|
||||
new : Builder
|
||||
new = Builder (Array.new 1) 0
|
||||
@ -743,6 +774,7 @@ type Builder
|
||||
|
||||
> Example
|
||||
Get the capacity of a new builder.
|
||||
|
||||
Vector.new_builder.capacity
|
||||
capacity : Integer
|
||||
capacity = this.to_array.length
|
||||
@ -754,6 +786,7 @@ type Builder
|
||||
|
||||
> Example
|
||||
Append an item to a vector builder.
|
||||
|
||||
Vector.new_builder.append 10
|
||||
append : Any -> Nothing
|
||||
append item = case this.capacity > this.length of
|
||||
@ -774,11 +807,13 @@ type Builder
|
||||
|
||||
> Example
|
||||
Use a builder to add elements to and then create a vector.
|
||||
bldr = Vector.new_builder
|
||||
bldr.append 1
|
||||
bldr.append 10
|
||||
bldr.append 100
|
||||
bldr.to_vector
|
||||
|
||||
example_to_vector =
|
||||
bldr = Vector.new_builder
|
||||
bldr.append 1
|
||||
bldr.append 10
|
||||
bldr.append 100
|
||||
bldr.to_vector
|
||||
to_vector : Vector
|
||||
to_vector =
|
||||
old_array = this.to_array
|
||||
@ -803,3 +838,4 @@ type Index_Out_Of_Bounds_Error index length
|
||||
Index_Out_Of_Bounds_Error.to_display_text : Text
|
||||
Index_Out_Of_Bounds_Error.to_display_text =
|
||||
"The index " + this.index.to_text + " is out of bounds in a vector with length " + this.length.to_text + "."
|
||||
|
||||
|
@ -2,7 +2,7 @@ from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Json
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Network.Http.Form
|
||||
import Standard.Base.Network.Http.Header
|
||||
import Standard.Base.Network.Http.Method
|
||||
|
86
distribution/std-lib/Standard/src/Examples.enso
Normal file
86
distribution/std-lib/Standard/src/Examples.enso
Normal file
@ -0,0 +1,86 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
|
||||
# Renamed to avoid clashing with an uppercase name resolution for `duration`.
|
||||
import Standard.Base.Data.Json as Enso_Json
|
||||
import Standard.Base.Data.List as Enso_List
|
||||
import Standard.Base.Data.Map as Enso_Map
|
||||
import Standard.Base.Data.Time.Duration as Enso_Duration
|
||||
|
||||
# Can do the top-level examples directory.
|
||||
|
||||
# Fine to not provide examples for some APIs (e.g. Database).
|
||||
|
||||
# Examples with HTTP should note this clearly in the example text.
|
||||
|
||||
# Image source should only download if it doesn't exist. We can say that the #
|
||||
# user can put it there.
|
||||
|
||||
# > Example
|
||||
# Frobnicate the doobly do
|
||||
#
|
||||
# import Standard.Examples
|
||||
#
|
||||
# my_example =
|
||||
# x = dlksaldjks
|
||||
# y = x + 1
|
||||
# ...
|
||||
|
||||
## An example error type used in a number of examples.
|
||||
|
||||
Arguments:
|
||||
- message: The message contained in the error type.
|
||||
type Example_Error_Type message
|
||||
|
||||
## An example CSV file for experimenting with Table and its APIs.
|
||||
csv : File
|
||||
csv = Enso_Project.data / "food_shop_inventory.csv"
|
||||
|
||||
## An example duration for experimenting with duration APIs.
|
||||
duration : Duration
|
||||
duration = Enso_Duration.between (Time.new 2020 10 20) Time.now
|
||||
|
||||
## An example amount of JSON as text.
|
||||
json_text : Text
|
||||
json_text = """
|
||||
[
|
||||
{
|
||||
"title": "Lord of the Rings",
|
||||
"author": {
|
||||
"name": "J. R. R. Tolkien",
|
||||
"year_of_birth": 1892
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "The Little Prince",
|
||||
"author": {
|
||||
"name": "Antoine de Saint-Exupéry",
|
||||
"year_of_birth": 1900
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "And Then There Were None",
|
||||
"author": {
|
||||
"name": "Agatha Christie",
|
||||
"year_of_birth": 1890
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
## Example JSON for working with.
|
||||
json : Enso_Json.Json
|
||||
json = Enso_Json.parse here.json_text
|
||||
|
||||
## An example JSON object.
|
||||
json_object : Enso_Json.Object
|
||||
json_object = here.json.items.head
|
||||
|
||||
## An example cons-list.
|
||||
list : Enso_List.List
|
||||
list = Cons 1 (Cons 2 (Cons 3 Nil))
|
||||
|
||||
## A simple map that contains some numbers mapped to their word equivalents.
|
||||
map : Enso_Map.Map
|
||||
map = Enso_Map.empty . insert 1 "one" . insert 3 "three" . insert 5 "five"
|
||||
|
@ -3,6 +3,7 @@ from Standard.Base import all
|
||||
import Standard.Table.Data.Table
|
||||
|
||||
polyglot java import org.enso.table.format.csv.Parser
|
||||
polyglot java import java.io.ByteArrayInputStream
|
||||
|
||||
## Reads the contents of `this` and parses them as a CSV dataframe.
|
||||
|
||||
@ -16,7 +17,71 @@ polyglot java import org.enso.table.format.csv.Parser
|
||||
inferred from the CSV header row or set manually.
|
||||
File.File.read_csv : Boolean -> Text -> Table
|
||||
File.File.read_csv has_header=True prefix='C' =
|
||||
here.from_csv this has_header prefix
|
||||
|
||||
## Reads a CSV and turns it into a table.
|
||||
|
||||
Arguments
|
||||
- has_header: Specifies whether the first line of the file should be
|
||||
interpreted as a header, containing storage names. If set to `False`,
|
||||
storage names will be automatically generated.
|
||||
- prefix: text that should be prepended to automatically generated storage
|
||||
names. For example, if `prefix` is set to `X`, the columns will be named
|
||||
`X0`, `X1`, etc. This argument has no effect if the storage name is
|
||||
inferred from the CSV header row or set manually.
|
||||
|
||||
> Example
|
||||
Read a CSV from disk and convert it into a table.
|
||||
|
||||
from Standard.Base import all
|
||||
import Standard.Table
|
||||
import Standard.Examples
|
||||
|
||||
example_csv_to_table =
|
||||
file = Examples.csv
|
||||
Table.from_csv file
|
||||
|
||||
> Example
|
||||
Read a CSV from memory and convert it into a table.
|
||||
|
||||
from Standard.Base import all
|
||||
import Standard.Table
|
||||
|
||||
example_csv_to_table =
|
||||
csv = """
|
||||
column_1, column_2, column_3
|
||||
1 , 2 , 3
|
||||
4 , 5 , 6
|
||||
Table.from_csv csv
|
||||
from_csv : File.File | Text -> Boolean -> Text -> Table ! Parse_Error
|
||||
from_csv csv has_header=True prefix='C' =
|
||||
parser_inst = Parser.create has_header prefix
|
||||
this.with_input_stream [File.Option.Read] stream->
|
||||
stream.with_java_stream java_stream->
|
||||
Table.Table (parser_inst.parse java_stream)
|
||||
|
||||
case csv of
|
||||
Text ->
|
||||
input_stream = ByteArrayInputStream.new csv.utf_8.to_array
|
||||
Table.Table (parser_inst.parse input_stream)
|
||||
File.File _ ->
|
||||
csv.with_input_stream [File.Option.Read] stream->
|
||||
stream.with_java_stream java_stream->
|
||||
Table.Table (parser_inst.parse java_stream)
|
||||
_ ->
|
||||
found_type_name = Meta.get_qualified_type_name csv
|
||||
file_name = Meta.get_qualified_type_name File.File
|
||||
text_name = Meta.get_qualified_type_name Text
|
||||
message = "Found type " + found_type_name + ", expected " + file_name + " or " + text_name + "."
|
||||
Error.throw (Parse_Error message)
|
||||
|
||||
## An error that occurs when the provided contents could not be parsed as a CSV.
|
||||
|
||||
Arguments:
|
||||
- message: The message that provides more details about the error.
|
||||
type Parse_Error message
|
||||
|
||||
## UNSTABLE
|
||||
|
||||
Converts the CSV parsing error into a
|
||||
Parse_Error.to_display_text : Text
|
||||
Parse_Error.to_display_text =
|
||||
"The input could not be parsed as a CSV: " + this.message
|
||||
|
||||
|
@ -16,9 +16,14 @@ these libraries, as well as notes on how they should be used.
|
||||
|
||||
- [Base](#base)
|
||||
- [Builtins](#builtins)
|
||||
- [Database](#database)
|
||||
- [Geo](#geo)
|
||||
- [Image](#image)
|
||||
- [Table](#table)
|
||||
- [Test](#test)
|
||||
- [Visualization](#visualization)
|
||||
- [Documentation](#documentation)
|
||||
- [Examples](#examples)
|
||||
- [General Libraries Notes](#general-libraries-notes)
|
||||
|
||||
<!-- /MarkdownTOC -->
|
||||
@ -28,7 +33,7 @@ these libraries, as well as notes on how they should be used.
|
||||
`Base` is the core library of Enso. It contains core types and data structures,
|
||||
as well as basic functionality for interacting with the outside world. It can be
|
||||
found in
|
||||
[`distribution/std-lib/Standard/src/`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
[`distribution/std-lib/Standard/src/Base.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
`Base` is intended to be imported unqualified at the top of the file:
|
||||
`from Standard.Base import all`. Items not included in this unqualified import
|
||||
@ -46,22 +51,44 @@ For the purposes of documentation, there is a
|
||||
file that provides stub definitions for these builtin functions. It is used for
|
||||
documentation purposes, and must be kept up to date as the builtins change.
|
||||
|
||||
These methods are re-exported from `Base` where appropriate, and should not be
|
||||
imported directly.
|
||||
|
||||
> #### Note: Shadow Definitions
|
||||
>
|
||||
> In time this file will be replaced by true shadow definitions for the language
|
||||
> builtins. It is only a stop-gap measure to allow documenting this
|
||||
> functionality at this point in time.
|
||||
|
||||
## Database
|
||||
|
||||
`Database` is a library that provides utilities for accessing data in databases
|
||||
and processing that data efficiently. It is part of the Enso standard libraries
|
||||
and is located in
|
||||
[`distribution/std-lib/Standard/src/Database.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
It is designed to be imported _qualified_.
|
||||
|
||||
## Geo
|
||||
|
||||
`Geo` is a library that contains very basic functionality for working with
|
||||
geographic data. We hope to expand it greatly in the future. It is located in
|
||||
[`distribution/std-lib/Standard/src/Geo.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
## Image
|
||||
|
||||
`Image` is a library that contains bindings to [OpenCV](https://opencv.org/)
|
||||
that allows users to work with image data. It is located in
|
||||
[`distribution/std-lib/Standard/src/Image.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
## Table
|
||||
|
||||
`Table` is Enso's dataframes library, providing functionality for loading and
|
||||
analysing tabular data. It is a core data-science toolkit, that integrates
|
||||
deeply with Enso and its IDE. It can be found in
|
||||
[`distribution/std-lib/Table`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Table).
|
||||
[`distribution/std-lib/Standard/src/Table.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
`Table` is designed to be imported unqualified: `from Table import all`. Items
|
||||
not included in this unqualified import are considered to be more specialist or
|
||||
internal, and should be intentionally imported by users.
|
||||
`Table` is designed to be imported qualified: `import Table`.
|
||||
|
||||
## Test
|
||||
|
||||
@ -69,27 +96,33 @@ internal, and should be intentionally imported by users.
|
||||
time it is _very_ rudimentary, and needs significant improvement before we can
|
||||
consider it an "official" part of the Enso standard libraries. It can be found
|
||||
in
|
||||
[`distribution/std-lib/Test`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Test).
|
||||
[`distribution/std-lib/Standard/src/Test.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
`Test` is intended to be imported qualified: `import Test`. This ensures that
|
||||
there aren't spurious name clashes between user-defined functionality and the
|
||||
testing library.
|
||||
|
||||
## Visualization
|
||||
|
||||
`Visualization` is a semi-internal library that provides visualization-specific
|
||||
utilities for displaying data in the IDE. It is located in
|
||||
[`distribution/std-lib/Standard/src/Visualization.enso`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src).
|
||||
|
||||
## Documentation
|
||||
|
||||
These libraries are comprehensively documented, with all functionality
|
||||
accompanied by comprehensive documentation comments. These are located _above_
|
||||
each definition, for example:
|
||||
|
||||
```
|
||||
```enso
|
||||
## Sort the Vector.
|
||||
|
||||
Arguments:
|
||||
- `on`: A projection from the element type to the value of that element
|
||||
- on: A projection from the element type to the value of that element
|
||||
being sorted on.
|
||||
- `by`: A function that compares the result of applying `on` to two
|
||||
- by: A function that compares the result of applying `on` to two
|
||||
elements, returning an Ordering to compare them.
|
||||
- `order`: The order in which the vector elements are sorted.
|
||||
- order: The order in which the vector elements are sorted.
|
||||
|
||||
By default, elements are sorted in ascending order, using the comparator
|
||||
`compare_to`. A custom comparator may be passed to the sort function.
|
||||
@ -97,11 +130,12 @@ each definition, for example:
|
||||
This is a stable sort, meaning that items that compare the same will not
|
||||
have their order changed by the sorting process.
|
||||
|
||||
The complexities for this sort are:
|
||||
- *Worst-Case Time:* `O(n * log n)`
|
||||
- *Best-Case Time:* `O(n)`
|
||||
- *Average Time:* `O(n * log n)`
|
||||
- *Worst-Case Space:* `O(n)` additional
|
||||
! Computational Complexity
|
||||
The complexities for this sort are:
|
||||
- *Worst-Case Time:* `O(n * log n)`
|
||||
- *Best-Case Time:* `O(n)`
|
||||
- *Average Time:* `O(n * log n)`
|
||||
- *Worst-Case Space:* `O(n)` additional
|
||||
|
||||
? Implementation Note
|
||||
The sort implementation is based upon an adaptive, iterative mergesort
|
||||
@ -115,13 +149,13 @@ each definition, for example:
|
||||
|
||||
> Example
|
||||
Sorting a vector of numbers.
|
||||
|
||||
[5, 2, 3, 45, 15].sort == [2, 3, 5, 15, 45]
|
||||
|
||||
> Example
|
||||
Sorting a vector of `Pair`s on the first element, descending.
|
||||
|
||||
[Pair 1 2, Pair -1 8].sort (_.first) (order = Sort_Order.Descending)
|
||||
sort : (Any -> Any) -> (Any -> Any -> Ordering) -> Sort_Order -> Vector
|
||||
sort (on = x -> x) (by = (_.compare_to _)) (order = Sort_Order.Ascending) = ...
|
||||
```
|
||||
|
||||
Such documentation blocks describe:
|
||||
@ -137,12 +171,31 @@ In addition, a function will have a type signature that describes the expected
|
||||
types of the function arguments. It may also have defaults for its arguments,
|
||||
which will be shown in the
|
||||
|
||||
### Examples
|
||||
|
||||
All documentation blocks in Enso should contain comprehensive examples for how
|
||||
to use the associated functionality. These documentation blocks fall into two
|
||||
types:
|
||||
|
||||
1. **Stand-Alone:** A stand-alone example is a single Enso expression that can
|
||||
be pasted into any method body and will execute.
|
||||
2. **Example Method:** An example method is a method named `example_*` that
|
||||
provides the example. They are used when specific imports are necessary to
|
||||
run the example, or when multiple lines are needed to provide an effective
|
||||
example.
|
||||
|
||||
All examples assume that the prelude is imported using
|
||||
`from Standard.Base import all` in the file into which it is being pasted.
|
||||
|
||||
The
|
||||
[`Standard.Examples`](https://github.com/enso-org/enso/tree/main/distribution/std-lib/Standard/src/Examples.enso)
|
||||
file contains example data for use in examples. If an example requires
|
||||
non-trivial data on which to operate, it should be placed here.
|
||||
|
||||
## General Libraries Notes
|
||||
|
||||
Some notes on the general structure of these libraries.
|
||||
|
||||
- All of these libraries are considered to be WIP as they are missing many
|
||||
pieces of functionality that they should have.
|
||||
- As the language doesn't currently have built-in support for access modifiers
|
||||
(e.g. `private`), so we instead use `PRIVATE` annotations at the top of
|
||||
documentation blocks. Any functionality annotated in such a form is not for
|
||||
|
@ -24,6 +24,9 @@ object Assoc {
|
||||
if (isApplicative(op)) Assoc.Left
|
||||
else if (op == "in") Assoc.Left
|
||||
else if (op == "<|") Assoc.Right
|
||||
else if (op == "|>") Assoc.Left
|
||||
else if (op == "<<") Assoc.Right
|
||||
else if (op == ">>") Assoc.Left
|
||||
else if (op.foldLeft(0)(_ + charAssoc(_)) >= 0) Assoc.Left
|
||||
else Assoc.Right
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ public class Text_Utils {
|
||||
* Checks whether {@code suffix} is a suffix of {@code str}.
|
||||
*
|
||||
* @param str the string to check
|
||||
* @param prefix the potential suffix
|
||||
* @param suffix the potential suffix
|
||||
* @return whether {@code suffix} is a suffix of {@code str}
|
||||
*/
|
||||
public static boolean ends_with(String str, String suffix) {
|
||||
|
62
test/Table_Tests/src/Csv_Spec.enso
Normal file
62
test/Table_Tests/src/Csv_Spec.enso
Normal file
@ -0,0 +1,62 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Table
|
||||
import Standard.Table.Io.Csv
|
||||
import Standard.Test
|
||||
|
||||
spec =
|
||||
c_1_data = [1, 4, 7, 10]
|
||||
c_2_data = [2, Nothing, 8, 11]
|
||||
c_3_data = [Nothing, 6, 9, 12]
|
||||
|
||||
c_1 = Json.from_pairs [["name", "a"], ["data", c_1_data]]
|
||||
c_2 = Json.from_pairs [["name", "b"], ["data", c_2_data]]
|
||||
c_3 = Json.from_pairs [["name", "c"], ["data", c_3_data]]
|
||||
|
||||
Test.group "Table.from_csv" <|
|
||||
Test.specify "should create a table from a textual CSV" <|
|
||||
file_contents = (Enso_Project.data / "simple_empty.csv") . read
|
||||
table = Table.from_csv file_contents
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3]]]
|
||||
|
||||
table.to_json.should_equal expected
|
||||
|
||||
Test.specify "should create a table from a CSV in a file" <|
|
||||
file_contents = (Enso_Project.data / "simple_empty.csv")
|
||||
table = Table.from_csv file_contents
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3]]]
|
||||
|
||||
table.to_json.should_equal expected
|
||||
|
||||
Test.specify "should create an error when given the wrong input" <|
|
||||
Table.from_csv [] . should_fail_with Csv.Parse_Error
|
||||
|
||||
Test.group "Parsing" <|
|
||||
Test.specify "should parse a simple numeric table" <|
|
||||
simple_empty = (Enso_Project.data / "simple_empty.csv") . read_csv
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3]]]
|
||||
|
||||
simple_empty.to_json.should_equal expected
|
||||
|
||||
Test.specify "should correctly infer types of varied-type columns" <|
|
||||
varied_column = (Enso_Project.data / "varied_column.csv") . read_csv has_header=False
|
||||
c_1_data = ["2005-02-25", "2005-02-28", "4", "2005-03-02", Nothing, "2005-03-04", "2005-03-07", "2005-03-08"]
|
||||
c_2_data = ["2005-02-25", "2005-02-28", "2005-03-01", Nothing, "2005-03-03", "2005-03-04", "2005-03-07", "2005-03-08"]
|
||||
c_3_data = [1, 2, 3, 4, 5, Nothing, 7, 8]
|
||||
c_4_data = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
c_5_data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.25, 7.0, 8.0]
|
||||
c_6_data = ['1', '2', '3', '4', '5', '6.25', '7', 'osiem']
|
||||
|
||||
c_1 = Json.from_pairs [["name", "C0"], ["data", c_1_data]]
|
||||
c_2 = Json.from_pairs [["name", "C1"], ["data", c_2_data]]
|
||||
c_3 = Json.from_pairs [["name", "C2"], ["data", c_3_data]]
|
||||
c_4 = Json.from_pairs [["name", "C3"], ["data", c_4_data]]
|
||||
c_5 = Json.from_pairs [["name", "C4"], ["data", c_5_data]]
|
||||
c_6 = Json.from_pairs [["name", "C5"], ["data", c_6_data]]
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3, c_4, c_5, c_6]]]
|
||||
varied_column.to_json.should_equal expected
|
||||
|
@ -2,9 +2,11 @@ from Standard.Base import all
|
||||
|
||||
import Standard.Test
|
||||
|
||||
import Table_Tests.Table_Spec
|
||||
import Table_Tests.Column_Spec
|
||||
import Table_Tests.Csv_Spec
|
||||
import Table_Tests.Table_Spec
|
||||
|
||||
main = Test.Suite.run_main <|
|
||||
Column_Spec.spec
|
||||
Csv_Spec.spec
|
||||
Table_Spec.spec
|
||||
|
@ -16,39 +16,6 @@ My.frobnicate = case this of
|
||||
My x1 y1 -> My y1 x1
|
||||
|
||||
spec =
|
||||
Test.group "Parsing" <|
|
||||
Test.specify "should parse a simple numeric table" <|
|
||||
simple_empty = (Enso_Project.data / "simple_empty.csv") . read_csv
|
||||
c_1_data = [1, 4, 7, 10]
|
||||
c_2_data = [2, Nothing, 8, 11]
|
||||
c_3_data = [Nothing, 6, 9, 12]
|
||||
|
||||
c_1 = Json.from_pairs [["name", "a"], ["data", c_1_data]]
|
||||
c_2 = Json.from_pairs [["name", "b"], ["data", c_2_data]]
|
||||
c_3 = Json.from_pairs [["name", "c"], ["data", c_3_data]]
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3]]]
|
||||
|
||||
simple_empty.to_json.should_equal expected
|
||||
Test.specify "should correctly infer types of varied-type columns" <|
|
||||
varied_column = (Enso_Project.data / "varied_column.csv") . read_csv has_header=False
|
||||
c_1_data = ["2005-02-25", "2005-02-28", "4", "2005-03-02", Nothing, "2005-03-04", "2005-03-07", "2005-03-08"]
|
||||
c_2_data = ["2005-02-25", "2005-02-28", "2005-03-01", Nothing, "2005-03-03", "2005-03-04", "2005-03-07", "2005-03-08"]
|
||||
c_3_data = [1, 2, 3, 4, 5, Nothing, 7, 8]
|
||||
c_4_data = [1, 2, 3, 4, 5, 6, 7, 8]
|
||||
c_5_data = [1.0, 2.0, 3.0, 4.0, 5.0, 6.25, 7.0, 8.0]
|
||||
c_6_data = ['1', '2', '3', '4', '5', '6.25', '7', 'osiem']
|
||||
|
||||
c_1 = Json.from_pairs [["name", "C0"], ["data", c_1_data]]
|
||||
c_2 = Json.from_pairs [["name", "C1"], ["data", c_2_data]]
|
||||
c_3 = Json.from_pairs [["name", "C2"], ["data", c_3_data]]
|
||||
c_4 = Json.from_pairs [["name", "C3"], ["data", c_4_data]]
|
||||
c_5 = Json.from_pairs [["name", "C4"], ["data", c_5_data]]
|
||||
c_6 = Json.from_pairs [["name", "C5"], ["data", c_6_data]]
|
||||
|
||||
expected = Json.from_pairs [["columns", [c_1, c_2, c_3, c_4, c_5, c_6]]]
|
||||
varied_column.to_json.should_equal expected
|
||||
|
||||
Test.group "JSON serialization" <|
|
||||
Test.specify "should serialize all column types to correct JSON" <|
|
||||
c_1 = [1, 2, 3, Nothing]
|
||||
|
@ -1,8 +1,8 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
import Standard.Base.Data.Time.Zone
|
||||
import Standard.Test
|
||||
@ -44,12 +44,12 @@ spec =
|
||||
date . month . should_equal 1
|
||||
date . day . should_equal 1
|
||||
Test.specify "should parse custom format" <|
|
||||
date = Date.parse_format "1999 1 1" "yyyy M d"
|
||||
date = Date.parse "1999 1 1" "yyyy M d"
|
||||
date . year . should_equal 1999
|
||||
date . month . should_equal 1
|
||||
date . day . should_equal 1
|
||||
Test.specify "should throw error when parsing custom format" <|
|
||||
date = Date.parse_format "1999-01-01" "yyyy M d"
|
||||
date = Date.parse "1999-01-01" "yyyy M d"
|
||||
case date.catch (x -> x) of
|
||||
Time.Time_Error msg ->
|
||||
msg . should_equal "Text '1999-01-01' could not be parsed at index 4"
|
||||
|
@ -1,7 +1,7 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Test
|
||||
|
||||
spec =
|
||||
|
@ -7,8 +7,8 @@ import Tests.Data.Time.Time_Spec
|
||||
import Tests.Data.Time.Zone_Spec
|
||||
|
||||
spec =
|
||||
Zone_Spec.spec
|
||||
Time_Of_Day_Spec.spec
|
||||
Date_Spec.spec
|
||||
Time_Spec.spec
|
||||
Duration_Spec.spec
|
||||
Time_Of_Day_Spec.spec
|
||||
Time_Spec.spec
|
||||
Zone_Spec.spec
|
||||
|
@ -1,8 +1,8 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Date
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time.Time_Of_Day
|
||||
import Standard.Base.Data.Time.Zone
|
||||
import Standard.Test
|
||||
@ -15,7 +15,7 @@ spec =
|
||||
time . minute . should_equal 0
|
||||
time . second . should_equal 0
|
||||
time . to_seconds . should_equal 3600
|
||||
Test.specify "should handle erros when creating time" <|
|
||||
Test.specify "should handle errors when creating a time" <|
|
||||
case Time_Of_Day.new 24 0 0 . catch (x -> x) of
|
||||
Time.Time_Error msg ->
|
||||
msg . should_equal "Invalid value for HourOfDay (valid values 0 - 23): 24"
|
||||
@ -46,10 +46,10 @@ spec =
|
||||
result ->
|
||||
Test.fail ("Unexpected result: " + result.to_text)
|
||||
Test.specify "should parse custom format" <|
|
||||
time = Time_Of_Day.parse_format "12:30AM" "hh:mma"
|
||||
time = Time_Of_Day.parse "12:30AM" "hh:mma"
|
||||
time.to_seconds . should_equal 1800
|
||||
Test.specify "should throw error when parsing custom format" <|
|
||||
time = Time_Of_Day.parse_format "12:30" "HH:mm:ss"
|
||||
time = Time_Of_Day.parse "12:30" "HH:mm:ss"
|
||||
case time.catch (x -> x) of
|
||||
Time.Time_Error msg ->
|
||||
msg . should_equal "Text '12:30' could not be parsed at index 5"
|
||||
|
@ -1,7 +1,7 @@
|
||||
from Standard.Base import all
|
||||
|
||||
import Standard.Base.Data.Time.Duration
|
||||
import Standard.Base.Data.Time.Time
|
||||
import Standard.Base.Data.Time
|
||||
import Standard.Base.Data.Time.Zone
|
||||
import Standard.Test
|
||||
|
||||
@ -101,7 +101,7 @@ spec =
|
||||
result ->
|
||||
Test.fail ("Unexpected result: " + result.to_text)
|
||||
Test.specify "should parse custom format of zoned time" <|
|
||||
time = Time.parse_format "2020-05-06 04:30:20 UTC" "yyyy-MM-dd HH:mm:ss z"
|
||||
time = Time.parse "2020-05-06 04:30:20 UTC" "yyyy-MM-dd HH:mm:ss z"
|
||||
time . year . should_equal 2020
|
||||
time . month . should_equal 5
|
||||
time . day . should_equal 6
|
||||
@ -111,7 +111,7 @@ spec =
|
||||
time . nanosecond . should_equal 0
|
||||
time . zone . zone_id . should_equal "Etc/UTC"
|
||||
Test.specify "should parse custom format of local time" <|
|
||||
time = Time.parse_format "06 of May 2020 at 04:30AM" "dd 'of' MMMM yyyy 'at' hh:mma"
|
||||
time = Time.parse "06 of May 2020 at 04:30AM" "dd 'of' MMMM yyyy 'at' hh:mma"
|
||||
time . year . should_equal 2020
|
||||
time . month . should_equal 5
|
||||
time . day . should_equal 6
|
||||
@ -120,7 +120,7 @@ spec =
|
||||
time . second . should_equal 0
|
||||
time . nanosecond . should_equal 0
|
||||
Test.specify "should throw error when parsing custom format" <|
|
||||
time = Time.parse_format "2008-01-01" "yyyy-MM-dd'T'HH:mm:ss'['z']'"
|
||||
time = Time.parse "2008-01-01" "yyyy-MM-dd'T'HH:mm:ss'['z']'"
|
||||
case time.catch (x -> x) of
|
||||
Time.Time_Error msg ->
|
||||
msg . should_equal "Text '2008-01-01' could not be parsed at index 10"
|
||||
|
Loading…
Reference in New Issue
Block a user