Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1506 Implement maxIfEmpty and minIfEmpty methods for collections #1512

Merged
merged 1 commit into from
Sep 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions org.uqbar.project.wollok.lib/src/wollok/lang.wlk
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class Collection {
* [].max({ e => e.length() })
* => Throws error, list must not be empty
*/
method max(closure) = self.absolute(closure, { a, b => a > b })
method max(closure) = self.maxIfEmpty(closure, { throw new ElementNotFoundException("collection is empty") })

/**
* Answers the element that represents the maximum value in the collection.
Expand All @@ -291,6 +291,37 @@ class Collection {
* [].max() => Throws error, list must not be empty
*/
method max() = self.max({it => it})

/**
* Answers the element that is considered to be/have the maximum value,
* or applies a closure if the collection is empty.
* The criteria is given by a closure that receives a single element
* as input (one of the element). The closure must return a comparable
* value (something that understands the >, >= messages).
* The closure to execute when the collection is empty is given as a second
* argument.
*
* Example:
* ["a", "ab", "abc", "d" ].maxIfEmpty({ e => e.length() }, { "default" })
* => Answers "abc"
*
* [].maxIfEmpty({ e => e.length() }, { "default" })
* => Answers "default"
*/
method maxIfEmpty(toComparableClosure, emptyCaseClosure) = self.absolute(toComparableClosure, { a, b => a > b }, emptyCaseClosure)

/**
* Answers the element that is considered to be/have the maximum value,
* or applies a closure if the collection is empty.
* The criteria is by direct comparison of the elements.
* The closure to execute when the collection is empty is given as a second
* argument.
*
* Example:
* [11, 1, 4, 8, 3, 15, 6].maxIfEmpty({ 99 }) => Answers 15
* [].maxIfEmpty({ 99 }) => Answers 99
*/
method maxIfEmpty(emptyCaseClosure) = self.maxIfEmpty({it => it}, emptyCaseClosure)

/**
* Answers the element that is considered to be/have the minimum value.
Expand All @@ -305,8 +336,8 @@ class Collection {
* [].min({ e => e.length() })
* => Throws error, list must not be empty
*/
method min(closure) = self.absolute(closure, { a, b => a < b} )
method min(closure) = self.absolute(closure, { a, b => a < b }, { throw new ElementNotFoundException("collection is empty") })

/**
* Answers the element that represents the minimum value in the
* non-empty collection.
Expand All @@ -318,10 +349,41 @@ class Collection {
*/
method min() = self.min({it => it})

/**
* Answers the element that is considered to be/have the minimum value,
* or applies a closure if the collection is empty.
* The criteria is given by a closure that receives a single element
* as input (one of the element). The closure must return a comparable
* value (something that understands the >, >= messages).
* The closure to execute when the collection is empty is given as a second
* argument.
*
* Example:
* ["ab", "abc", "hello", "wollok world"].minIfEmpty({ e => e.length() }, { "default" })
* => Answers "ab"
*
* [].minIfEmpty({ e => e.length() }, { "default" })
* => Answers "default"
*/
method minIfEmpty(toComparableClosure, emptyCaseClosure) = self.absolute(toComparableClosure, { a, b => a < b }, emptyCaseClosure)

/**
* Answers the element that is considered to be/have the minimum value,
* or applies a closure if the collection is empty.
* The criteria is by direct comparison of the elements.
* The closure to execute when the collection is empty is given as a second
* argument.
*
* Example:
* [11, 1, 4, 8, 3, 15, 6].minIfEmpty({ 99 }) => Answers 1
* [].minIfEmpty({ 99 }) => Answers 99
*/
method minIfEmpty(emptyCaseClosure) = self.minIfEmpty({it => it}, emptyCaseClosure)

/** @private */
method absolute(closure, criteria) {
method absolute(closure, criteria, emptyCaseClosure) {
if (self.isEmpty())
throw new ElementNotFoundException("collection is empty")
return emptyCaseClosure.apply()
const result = self.fold(null, { acc, e =>
const n = closure.apply(e)
if (acc == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ class CollectionTestCase extends AbstractWollokInterpreterTestCase {
assert.throwsException({[].min()})
'''.test
}

@Test
def void minIfEmpty() {
'''
«instantiateStrings»
assert.equals('hi', strings.minIfEmpty({ e => e.length() }, { 'lista vacia' }))
assert.equals('lista vacia', [].minIfEmpty({ e => e.length() }, { 'lista vacia' }))
'''.test
}

@Test
def void minIfEmptyNoArgs() {
'''
assert.equals(1, [3,1,2].minIfEmpty({ 99 }))
assert.equals(99, [].minIfEmpty({ 99 }))
'''.test
}

@Test
def void max() {
Expand All @@ -64,6 +81,23 @@ class CollectionTestCase extends AbstractWollokInterpreterTestCase {
'''.test
}

@Test
def void maxIfEmpty() {
'''
«instantiateStrings»
assert.equals('bonjour', strings.maxIfEmpty({ e => e.length() }, { 'lista vacia' }))
assert.equals('lista vacia', [].maxIfEmpty({ e => e.length() }, { 'lista vacia' }))
'''.test
}

@Test
def void maxIfEmptyNoArgs() {
'''
assert.equals(3, [1,3,2].maxIfEmpty({ 99 }))
assert.equals(99, [].maxIfEmpty({ 99 }))
'''.test
}

@Test
def void size() {
'''
Expand Down