-
Notifications
You must be signed in to change notification settings - Fork 0
/
Function_definition.fold
1379 lines (974 loc) · 28.3 KB
/
Function_definition.fold
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--
-- Function definition
--
-- Just say hello.
function hello name =
print "Hello, $name!"
-- Hello function with type declaration.
val hello :: String -> ()
fun hello name =
print "Hello, $(name)!"
-- Hello function with type annotation.
function hello name :: String -> () =
print "Hello, $(name)!"
-- Lambda greeting, functions are just lambda values.
val hello = name ->
print "Hello, $name!"
-- Function with composed body.
fun hello name =
let msg = "Hello, $name!" in
print "What's up?."
-- Lambda greeting with a block body.
val hello =
do name ->
print "Hello, $name!";
print "What's up?."
end
-- Factorial and fibonacci with argument pattern matching.
function
| factorial 0 = 1
| factorial n = n * factorial (n - 1)
-- 1
def factorial 0 = 1
| factorial n = n * factorial (n - 1)
-- 2
def factorial 0 => 1
| factorial n => n * factorial (n - 1)
function
| fibonacci 0 = 0
| fibonacci 1 = 1
| fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
-- Derivatives
val eps = 0.0001
fun derivative f =
x -> (f (x + eps) - f x) / eps
fun square x = x * x
fun around delta, target =
actual -> abs (actual - target) < delta
val () =
let dsquare = derivative square in
assert (dsquare 5 |> satisfies (around 0.1, 10));
assert (dsquare 10 |> satisfies (around 0.1, 20))
-- Constructs a list with numbers from `start` to `stop`.
let range from: (start = 0) to: stop by: (step = 1) =
if start >= stop then []
else [start & range from: (start + 1) to: stop by: step]
range :: from: Int? -> to: Int -> by: Int? -> [Int]
-> range to: 100
:: [Int] = [1, 2, 3, 4, 5, ... 99, 100]
-> range from: 2, to: 10, by: 3
:: [Int] = [2, 5, 8]
let take_while p [] = []
| take_while p [x & xs] =
if p x then [x & take_while p xs] else []
let take_while p [] => []
| take_while p [x & xs] =>
if p x then [x & take_while p xs] else []
-- -- --
map file_list, read \file
map file_list (read \file)
-- In a simple curried def definition:
f x y z = ...
-- The second token, `f` is by default parsed as a parameter annotation.
-- Consider the following example:
div x y
-- Is parsed as:
-> div x (by y)
-- ^ ^ ^
-- | | |
-- | | `- annotated parameter name
-- | `- simple parameter name
-- `- def name
-- With types:
(div x::Int) (y::Int)
-- defs with more than two parameters:
def range start stop step
-- Here is the version with types and default values.
def range (start::Int = 0) (stop::Int) (step::Int = 1)
-- The defcton can be called like this:
range 100 -- Only the `stop` argument passed.
range 0 100 -- First two arguments passed, the `step` is default.
range 0 100 2 -- Positional def application.
`
-- The last example's readability can be improved with parameter annotations.
range (from start = 0) (to stop::Int) (by step::Int = 1) =
...
'
range :: from: Int -> to: Int -> by: Int -> [Int]
range (from start = 0) (to stop) (by step = true) =
range (from start = 0) (to stop::Int) (by step::Int = 1)
case (fetch last_node: n1) of:
| Node (3, 2 Tree (Node)) -> "This is what we need."
| Node (8, 1 Node) -> "This is different."
| * -> "No, no"
if (x > 3) then:
"Hello!"
eles: "Not good."
-- Equivalent because removing then still requires one arg after.
if (x > 3):
print "Hello!" "World" sep: "\n";
eles:
"Not good."
-> range to: 100
-> range from: 0 to: 100 by: 2
-- If the parameter annotation and the actual parameter name are the same:
def hello (name name::String)
hello name: "Robot"
-- Instead, just add a `~` in front of the parameter name as a shortcut:
def hello (~name::String)
hello name: "Robot"
-- If you have a variable in scope with the same name as a parameter annotation,
-- you can use `~` to directly apply it.
name = "Robot"
hello name: name
hello name
!@#$%^&*_+\'/.,><~`
-- Instead of:
name = %"Robot %(full_name # 1)"
hello ~name: name
def (view::View) did_update context::Context with_opitions::({String -> Int}?) = {
...
}
my_view did_update my_ctx with_opitions {...}
my_view did_update my_ctx!
map (_ + _) (range 100)
type units =
| Metric [@name "metric"]
| Imperial [@name "imperial"]
@(deriving yojson)
type Units =
| Metric @(name "metric")
| Imperial @(name "imperial")
deriving: Yojson
let%lwt x = 3
[%lwt let x = 3]
name = "ppx_string"
mood = "fine"
larrys_baby = "perl"
msg = [%str "hello $(name) are you $(mood)?\n"]
print msg
print [%str {"
This also works with new string syntax
So you can do templates like in $(larrys_baby).
"}];
print [%str "testing double dollar: $$(name)"];
print_newline()
type Longident = [%import: Longident.t] deriving: Show
let () =
print_endline (show_longident (Longident.parse "Foo.Bar.baz"))
-- If we allow keyword parameters to be also used positionally (perhaps we
-- should do this), we retain all of traditional positional usage and rules.
-- Using Evan's example:
f : from:Int -> String
g : to:Int -> String
h : Int with: Int -> String
f from: 2 -- fine, returns String
f 2 -- also fine, returns String
f to: 2 -- type error
h 2 -- fine, returns (with: Int -> String)
h with:2 -- fine, returns (Int -> String)
h 1 2 -- fine, returns Int
okay = map (\f -> f 10) [f,g] -- types OK
what = map (\f -> f (to:10)) [f,g] -- type error
move : String -> from: Int -> to: List{Int} -> Result
move "a" from: 1 to: [2] -- Result
3 immediate and readable curried forms without need for "flips":
move "a" -- from:Int -> to:List[Int] -> Result
move from:1 -- String -> to:List[Int] -> Result
move to:[2] -- String -> from:[Int] -> Result
move :: String -> from: Int -> to: [Int] -> Result
`(move:from:to)
sum : Int -> Int -> Int
Int -> div : Int -> Int
3 div 2
(+)
(if: then: else:)
if:then:else ::
if: Bool -> then: Lazy a -> else: Lazy a =
_+_ : ℕ → ℕ → ℕ
zero + m = m
(suc n) + m = suc (n + m)
(+) : ℕ → ℕ → ℕ
zero + m = m
(suc n) + m = suc (n + m)
--
sum x y = x + y
sum(x, y) = x + y
sum[x, y] = x + y
sum x, y = x + y
--
sum(2, 3)
sum[2, 3]
--
let visits_durations_by_device :: List Visits -> List (Device, List Time)
let visits_durations_by_device visits_by_device =
let durations = map (x -> (last x) - (first x)) in
map (dev visit -> (dev, durations visit)) visits_by_device
--
-- Space-based application
IO.write ((String.join (map show args) with: sep) + end) to: IO.stdout
if (detections_count < settings.min_count):
log warning "Not enough detections."
else:
log status "Captured `~detections_count` detections."
-- Space-based application with commas
IO.write (String.join (map show args), with: sep) + end, to: IO.stdout
if (detections_count < settings.min_count),
log warning "Not enough detections."
else:
log status "Captured `~detections_count` detections."
-- Parentheses as application operator
IO.write(String.join(map show args, with: sep) + end, file: IO.stdout)
get "/analytics/<metric_name::String>/<date::Date>":
div: h1 {style = {font = Helvetica}}:
request.metric_name
end
end
"Returns a sequence of numbers from start (inclusive) to end (exclusive),
by step, where start defaults to 0, step to 1."
def range from: (start::Int = 0) to: (stop::Int) by (step::Int = 1) -> [Int] =
if (start == stop)
[]
else
[start & range from: (start + 1) to: stop by: step]
def square n:
n * n
end
square n = n * n
square = n -> n * n
def square n:
log "Will calculate square..."
n * n
end
square n = do
log "Will calculate square..."
n * n
end
square n = do
x <- read_str
return x
end
square = n -> do
log "Will calculate square..."
n * n
end
let assoc (v::PersistentVector a) (i::Int) (el::a) -> PersistentVector =
check_bounds! v i;
if i > (length v - length v.tail) then
let newtail = v.tail # [1 .. length v.tail] in
newtail # (mask i) := el;
PersistentVector v.trie newtail v.length
else
let newnode = v.trie # i # [1..end] in
newnode # (mask i) := el;
PersistentVector (assoc v.trie i newnode) v.tail v.length
let visits_durations_by_device :: Dict Device (List Visit) -> Dict Device (List Time)
let visits_durations_by_device visits_by_device =
let duration v = last v - first v in
Dict.map (map duration) visits_by_device
val x = 1
fun sum ctx xs =
let hourly_counts_by_access =
xs |> group_by_access
|> add_missing_accesses \accesses (Spaces.obtain_access_list ctx.space)
|> add_missing_hours_by_access ctx.date
let sum_by_access = map hourly_counts_by_access
in
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
---
type List a =
| Cons (a, list a)
| Nil
let length self =
match self
| Cons (_, tail) -> 1 + length tail
| Nil -> 0
end
let list_1 = Cons (11, Cons (22, Cons (33, Nil)))
assert (length test_list == 3)
def append l1 l2 = l1 . {
Nil -> l2
Cons (x, tail) -> Cons (x, append tail l2)
}
list_2 = append(test_list, Cons (44, Nil))
list_2 == Cons (11, Cons (22, Cons (33, Cons (44, Nil))))
-- -- --
type List T =
| Cons (T, List T)
| Nil
def length =
| Cons (_, tail) -> 1 + length tail
| Nil -> 0
end
list_1 = Cons (11, Cons (22, Cons (33, Nil)))
length test_list == 3
def append l1 l2 =
case l1:
Nil -> l2
Cons (x, tail) -> Cons (x, append tail l2)
end
end
list_2 = append test_list (Cons (44, Nil))
list_2 == Cons (11, Cons (22, Cons (33, Cons (44, Nil))))
-- -- --
(def hello [name]
(print ("Hello" + name)))
(def hello
[] -> (hello "World")
[name] -> (print ("Hello" + name)))
-- -- --
def hello name
print ("Hello" + name)
end
def
hello
[] -> (hello "World")
[name] -> (print ("Hello" + name)))
end
def hello
-> hello "World"
name -> print ("Hello" + name)
end
-- -- --
retry callback timeout retries = ...
retry = callback timeout retries ->
Log#info
repeat retries (-> callback >>= wait for: 5.Time#seconds)
-- -- --
def run_seq_command :: [Int] -> A -> Bool -> Int -> Unit
def run_seq_command nslots ready n =
| Done r -> do
Log.info "Job %a finished" (slog (string_of_int << V.hash)) n
results = M.append n r results
running = M.remove n running
if not (M.is_empty running)
print_status (M.cardinal results) running
end
new_ready = (Set (G.succ g n) ++ mutual_exclusion_set n)
|> filter (n -> all (n -> M.mem n results) (G.pred g n) &&
empty? (mutual_exclusion_set n \\ map_keys running))
loop (nslots + 1) results running (ready ++ new_ready)
end
| Run (cmd, cont) -> do
log "Next task in job %a: %a" (slog (string_of_int @* V.hash)) n
(slog OpamProcess.string_of_command) cmd;
if OpamProcess.is_verbose_command cmd ||
not (OpamGlobals.disp_status_line ()) then
OpamMisc.Option.iter
(OpamGlobals.msg "%s Command started\n")
(OpamProcess.text_of_command cmd);
let p =
if dry_run then OpamProcess.dry_run_background cmd
else OpamProcess.run_background cmd
in
let running =
M.add n (p, cont, OpamProcess.text_of_command cmd) running
in
print_status (M.cardinal results) running;
loop nslots results running ready
end
-- -- --
[1, 2, 3, 4].map {|x| x > 2 }.first
[1, 2, 3, 4].map (x -> x > 2).first
[1, 2, 3, 4] >> map (x -> x > 2) >> first
[1, 2, 3, 4] -> map (x -> x > 2) -> first
[1, 2, 3, 4] -> map (x -> x > 2) -> first
[1, 2, 3, 4] |> map (x -> x > 2) |> first
[1, 2, 3, 4] | map { \x > 2 } | first
-> [1, 2, 3, 4]
(where { \x > 2 })
first
select first from: [1, 2, 3, 4] where: x -> x > 2
-- -- --
open Str
let read_line ch =
Some (input_line ch)
catch End_of_file -> None
let fold_channel f acc ch =
match read_line ch
| Some line -> fold_channel f (f line acc) ch
| None -> acc
end
def add_to totals values =
case totals
| [] -> values
| _ -> if length totals == length values
then: map (+) totals values
else: fail "Inconsistent-length rows"
end
|| Sums all the values from channel.
def sum_channel ch
values_of_line line = map (::Float) (split line on: ',')
fold_channel folder [] ch where
folder line acc = add_to acc (values_of_line line)
end
end
|| Reads numbers from a CSV file located at <file_path> and sums all results.
|| The output is printed in a CSV format to standard output.
def sum_and_print_file file_path::String -> Unit
with chan <- open file_path mode: Read_only
result = sum_channel chan
print : join (map (::String) result) with: "," ++ "\n"
end
end
def main
| [_app, file_path] -> sum_and_print_file file_path
| _ -> fail "usage: sum_csv <file>"
end
main Sys#args
-- -- --
let access_counts_to_string access_counts =
String.Map.fold access_counts ~init:""
~f:(def ~key:access ~data:hourly_counts access_str ->
List.fold hourly_counts ~init:access_str ~f:(def hours_str (h, c) ->
hours_str ^ (format "%s, %d, %d\n" access (Float.to_int (Time.to_float h)) c)))
def access_counts_to_string access_counts
String.Map.fold access_counts
init: "" f: \(key access) (data hourly_counts) access_str ->
List.fold hourly_counts init: access_str f: \hours_str (h, c) ->
hours_str ++ ("%s, %d, %d\n" % (access, Float.to_int (Time.to_float h), c))
end
-- -- --
def x y -> x + y end
fn: x y -> x + y
type Tree
Leaf
Node (Int, Left, Right)
end
type Tree: do
Leaf
Node (Int, Left, Right)
end
detections -> length
detections -> length
detections >> length
detections |> length
detections -> ([] -> [x | xs] -> 1 + length xs)
detections -> ([] -> [x | xs] -> 1 + length xs)
detections |> ([] -> [x | xs] -> 1 + length xs)
detections :- ([] -> [x | xs] -> 1 + length xs)
detections :> ([] -> [x | xs] -> 1 + length xs)
detections # ([] -> [x | xs] -> 1 + length xs)
detections . ([] -> [x | xs] -> 1 + length xs)
detections & ([] -> [x | xs] -> 1 + length xs)
detections | ([] -> [x | xs] -> 1 + length xs)
detections > ([] -> [x | xs] -> 1 + length xs)
-> name
-- -- --
|| Creates a new object given a message passing primitive.
|| This is very good def.
def mk_object n_vs
msg ->
def lookup locals
case locals
| Empty -> raise: "Message not found: " + msg
| Link f r -> if (f.name == m): f.value else: lookup(r)
end
lookup n_vs
end
end
lookup <A> :: Self >> Formula -> Bool -> Int -> A
def self >> lookup attrs recursive level
...
end
db -> lookup
db >> lookup ("city" == "London" and "age" > 25)
: True : 2
db -> lookup ("city" == "London" and "age" > 25)
$ True $ 2
def db -> lookup
|| Transforms the integer index in content object by selecting the adequate
|| content type of the provided index.
def <Content_type::Collection_type where Content_type.Index::Forward_indext_type>
(integer_index_into_content content::Content_type content_index::Int)
-> Content_type.Element
...
end
|| Transforms the integer index in content object by selecting the adequate
|| content type of the provided index.
def <Content_type::Collection_type where Content_type.Index::Forward_indext_type>
integer_index_into_content content::Content_type content_index::Int
-> Content_type.Element
...
end
-- -- --
def sum ctx xs
hourly_counts_by_access =
xs >> group_by_access
>> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
>> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
["Susanovska", "Riszovsky"]
>- tonhos -> escolher_um_tonho_a_sorte tonhos
>- pedir_para_dizer_um_disparate >> rir_se_do_disparate disparate
plano = rir_se_do_disparate disparate << pedir_para_dizer_um_disparate
dizer_ola = nome -> print "Olá, " + nome
def sum ctx xs =
let hourly_counts_by_access xs =
xs |> group_by_access
|> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
|> add_missing_hours_by_access ctx.date
let sum_by_access =
map (hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
(hourly_counts_by_access xs)
in
sum_by_access
def sum ctx xs
hourly_counts_by_access =
xs -> group_by_access
-> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
-> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs >> group_by_access
>> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
>> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs -> group_by_access
-> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
-> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def calc_sum ctx xs
hourly_counts_by_access =
xs -> group_by_access
-> xs -> add_missing_accesses xs (Spaces.obtain_access_list)
-> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map (#2) hourly_counts)))
sum_by_access
end
def self -> calc_sum xs
xs -> group_by_access
-> add_missing_accesses (Spaces.obtain_access_list!)
-> add_missing_hours_by_access self.date
-> map ((#2) >> (\case | [] | [x] -> `Sum 0
| counts -> `Sum (fold (+) 0 counts))
end
def calc_sum ctx xs
xs | group_by_access
| add_missing_accesses (Spaces.obtain_access_list!)
| add_missing_hours_by_access ctx.date
| map ((#2) >> (counts -> `Sum (fold (+) 0 counts)))
end
def sum ctx xs :: App.Context -> List (Time, Int) =
xs |> group_by_access
|> add_missing_accesses (Spaces.obtain_access_list ())
|> add_missing_hours_by_access ctx.date
|> map (\item #2 >> fold (+) 0 >> `Sum))
def calc_sum ctx xs
xs -> group_by_access
-> add_missing_accesses (Spaces.obtain_access_list!)
-> add_missing_hours_by_access ctx.date
-> map ((#2) >> (counts -> `Sum (fold (+) 0 counts)))
end
def self -> calc_sum ctx
self -> group_by_access
-> add_missing_accesses (Spaces.obtain_access_list!)
-> add_missing_hours_by_access ctx.date
-> map ((#2) >> (counts -> `Sum (fold (+) 0 counts)))
end
def calc_sum ctx xs
hourly_counts_by_access =
xs -> group_by_access
-> add_missing_accesses \xs (Spaces.obtain_access_list)
-> add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(\hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs >- group_by_access
>- xs -> add_missing_accesses xs (Spaces.obtain_access_list)
>- improve_the_format >> prepare_for_operation
>- add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs | group_by_access
| xs -> add_missing_accesses xs (Spaces.obtain_access_list)
| add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs . group_by_access
. xs -> add_missing_accesses xs (Spaces.obtain_access_list)
. add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
def sum ctx xs
hourly_counts_by_access =
xs # group_by_access
# xs -> add_missing_accesses xs (Spaces.obtain_access_list)
# add_missing_hours_by_access ctx.date
sum_by_access = map hourly_counts_by_access
(hourly_counts -> `Sum (fold (+) 0 (map #2 hourly_counts)))
sum_by_access
end
process = >> add_missing_accesses >> add_missing_hours_by_access
<* <*> <+> <$> *** <|> !! ||
=== =-> <<< >>> <> +++ <- ->
-> >> << >>= =<< .. ... ::
-< >- -<< >>- ++ /= ==
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- VIEW
view :: (Int, Int) -> Turtle -> Element
view (w,h) turtle =
let turtle_pic =
to_form (image 96 96 "/turtle.gif")
▷ rotate turtle.angle
▷ move (turtle.x,turtle.y)
in
layers
[ collage w h [turtle_pic]
, opacity 0.7 <| fitted_image w h "/water.gif"
]
-- VIEW
let view :: (Int, Int) -> Turtle -> Element
let view (w,h) turtle =
let turtle_pic =
to_form (image 96 96 "/turtle.gif")
|> rotate turtle.angle >> not true
|> move (turtle.x, turtle.y)
in
layers
[collage w h [turtle_pic],
opacity 0.7 : fitted_image w h "/water.gif"]
[1, 2, 3, 4, 5] |> length
-- -- --
type Space = Int
show : Space -> Str
show = space -> show (Int space)
instance Show Space
show = show <Str>
end
let group_devices_by_store :: Space -> Map Store (Set Device)
let group_devices_by_store space =
let store_distances_by_antenna = get_store_distances space
let find_stores =
find_stores_for_antenna store_distances_by_antenna
in
IO.stdin
|> Text.lines
|> List.fold init: Store_map.empty
do device_set_by_store line ->
let {antenna, device, rssi} = Row.parse line in
fold (find_stores antenna rssi) init: device_set_by_store
do device_set_by_store' store ->
Store_map.change device_set_by_store' store
(some << (do None -> {}
| Some device_set -> device @ device_set
end))
end
end
group_devices_by_store :: Space -> Map Store (Set Device)
group_devices_by_store -> fn space ->
let store_distances_by_antenna = get_store_distances space,
find_stores = find_stores_for_antenna store_distances_by_antenna
in
In_channel.stdin -> lines -> fold init: Store_map.empty
do device_set_by_store line ->
let {antenna, device, rssi} = Row.parse line
in
fold (find_stores antenna rssi) init: device_set_by_store
\device_set_by_store' store ->
Store_map.change device_set_by_store' store
(some << (\case | None -> {}
| Some device_set -> device @ device_set))