Commit
This lets us share functionality between List and Range (and other user-defined sequence types).
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
class List { | ||
class Sequence { | ||
map (f) { | ||
var result = [] | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
munificent
Author
Member
|
||
for (element in this) { | ||
result.add(f.call(element)) | ||
} | ||
return result | ||
} | ||
|
||
where (f) { | ||
var result = [] | ||
This comment has been minimized.
Sorry, something went wrong. |
||
for (element in this) { | ||
if (f.call(element)) result.add(element) | ||
} | ||
return result | ||
} | ||
} | ||
|
||
class List is Sequence { | ||
toString { | ||
var result = "[" | ||
for (i in 0...count) { | ||
|
@@ -16,20 +34,6 @@ class List { | |
} | ||
return result | ||
} | ||
|
||
map (f) { | ||
var result = [] | ||
for (element in this) { | ||
result.add(f.call(element)) | ||
} | ||
return result | ||
} | ||
|
||
where (f) { | ||
var result = [] | ||
for (element in this) { | ||
if (f.call(element)) result.add(element) | ||
} | ||
return result | ||
} | ||
} | ||
|
||
class Range is Sequence {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
var a = [1, 2, 3] | ||
var inc = fn (x) { return x + 1 } | ||
var b = a.map(inc) | ||
|
||
var b = a.map(fn (x) x + 1) | ||
IO.print(b) // expect: [2, 3, 4] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
IO.print([] is List) // expect: true | ||
IO.print([] is Sequence) // expect: true | ||
IO.print([] is Object) // expect: true | ||
IO.print([] is Bool) // expect: false | ||
IO.print([].type == List) // expect: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
var a = [1, 2, 3] | ||
var moreThan1 = fn (x) { return x > 1 } | ||
var moreThan10 = fn (x) { return x > 10 } | ||
var b = a.where(moreThan1) | ||
var c = a.where(moreThan10) | ||
|
||
var b = a.where(fn (x) x > 1) | ||
IO.print(b) // expect: [2, 3] | ||
|
||
var c = a.where(fn (x) x > 10) | ||
IO.print(c) // expect: [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var a = 1..3 | ||
var b = a.map(fn (x) x + 1) | ||
IO.print(b) // expect: [2, 3, 4] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
var range = 2..5 | ||
|
||
IO.print(range is Range) // expect: true | ||
IO.print(range is Sequence) // expect: true | ||
IO.print(range is Object) // expect: true | ||
IO.print(range is String) // expect: false | ||
IO.print(range.type == Range) // expect: true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var a = 1..3 | ||
var b = a.where(fn (x) x > 1) | ||
IO.print(b) // expect: [2, 3] | ||
|
||
var c = a.where(fn (x) x > 10) | ||
IO.print(c) // expect: [] |
7 comments
on commit f8a9d7f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, would Collection
be a better name? Alternatively, Iterable
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collection
feels wrong to me because that implies that the class stores values, which isn't always the case. (For example, Range
doesn't.) Iterable
is the more typical name. It's what Java and Dart use, but it's also an odd word. I thought I'd try Sequence
and see how it feels. (For what it's worth, Clojure uses ISeq
for this, and Scheme uses "sequence".)
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to add too much to the hierarchy, but I was thinking Iterable
would provide iterator, iteratorValue, where,
and map
, and Collection
would provide add, clear
, etc. and be an Iterable
. Set
would be a Collection
but is unordered so it shouldn't be a Sequence
. Range
would be Iterable
but not Collection
.
Scala's hierarchy approximately follows this (It doesn't have a Collection
, but provides other layers): http://docs.scala-lang.org/overviews/collections/overview.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of the luxuries of being dynamically typed is that we don't have to build a type hierarchy so that users can use them as type annotations. For example, in Scala, if you want a method that takes an object and calls clear
and iterator
on it, you need some Collection
interface that specifies those.
In Wren, since we're duck typed, we only need base classes to share actual implementations of methods. So we have Sequence
as a place to share where
and map
. For things like add
and clear
, each concrete type will likely have its own implementation, so we probably don't need a base class for them.
This is new territory for me. My background is big complex OOP hierarchies, so I kind of like that stuff. But with Wren, I'm trying hard to deliberately keep things as simple as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's reasonable. Semantically, I just feel weird saying Set is Sequence
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For future reference, Smalltalk: http://www.jmcsweeney.co.uk/computing/m206/collections_intro.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, Set is Sequence
does feel a bit weird, but I feel like "sequence" is otherwise a nicer, more concrete name for that abstraction. I'm half convinced to switch it to "iterable" but let's try "sequence" for a while and see if we warm up to it. It'll be easy to change for a while.
Should this be
new this.type
so that this doesn't return a list each time? Implementing classes would then need to provide an add method.