Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 2-Io/day1-singletons.io @ 93:39084e2b8744
Add a function for word-aware text wrapping
Potentially hugely inefficient because we iterate through the
string character by character, but then splitting it first and
iterating over words still needs to iterate over the string to
know where to split.
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 18 Jun 2019 21:05:00 +0100 |
parents | 3d41d9d72cc9 |
children |
rev | line source |
---|---|
34
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 # Conditions are simple and like most languages |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 # Not mentioned yet, but found by necessity: we can use brackets! |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 (4 < 5) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 # Unlike Python, Io uses lower case for booleans |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 (true and false) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 # Truthy values are more like Python than C: |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 (true and 6) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 (true and 0) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 # But true, false and nil are all just objects |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 # Albeit special "singleton" objects where cloning gets the same object |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 true clone println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 false clone println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 nil clone println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 (true == true clone) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
17 # We can create our own singletons by altering the clone method to return the class |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 # Highlander: THERE CAN BE ONLY ONE! |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 Highlander := Object clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 Highlander clone := Highlander |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 Highlander println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 Highlander clone println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 highlander1 := Highlander clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 highlander2 := Highlander clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 (highlander1 == highlander2) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 # Normally cloning gives different objects with different IDs |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 obj1 := Object clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 obj2 := Object clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 (obj1 == obj2) println |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 # And with great power comes great responsibility. |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 # Object is not special. It is not protected in any way. |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 # DO NOT do this in real code! |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 Object clone := "Ooops!" |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 obj3 := Object clone |
3d41d9d72cc9
Add Day 1 code for Singletons (including booleans)
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
36 obj3 println |