A callback function that accepts an argument. The method calls the predicate
one time for each element in object.
An object to which the this
keyword can refer in the predicate
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element yielding the largest value of the given function or throws KoconutNoSuchElementException if there are no elements.
1.0.10
Use maxByOrNull instead.
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const largestNumberOfArray = await koconutArray
.maxBy(eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray)
// ↑ 5
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxBy(eachNumber => eachNumber)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const longestStringOfSet = await koconutSet
.maxBy(eachString => eachString.length)
.yield()
console.log(longestStringOfSet)
// ↑ abc
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1, 12, 123)
.associateWith(eachNumber => eachNumber.toString())
const longestDigitsEntryOfMap = await koconutMap
.maxBy(eachEntry => eachEntry.value.length)
.yield()
console.log(longestDigitsEntryOfMap)
// ↑ Entry { keyElement: 123, valueElement: '123' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(19,27,32)
const largestNumberOfArray2 = await koconutArray2
.maxBy(async eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray2)
// ↑ 32
const largest1sDigitNumberOfArray2 = await koconutArray2
.maxBy(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(largest1sDigitNumberOfArray2)
// ↑ 19
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element yielding the largest value of the given function or null if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const largestNumberOfArray = await koconutArray
.maxByOrNull(eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray)
// ↑ 5
const largestNumberOfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxByOrNull(eachNumber => eachNumber)
.yield()
console.log(largestNumberOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const longestStringOfSet = await koconutSet
.maxByOrNull(eachString => eachString.length)
.yield()
console.log(longestStringOfSet)
// ↑ abc
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1, 12, 123)
.associateWith(eachNumber => eachNumber.toString())
const longestDigitsEntryOfMap = await koconutMap
.maxByOrNull(eachEntry => eachEntry.value.length)
.yield()
console.log(longestDigitsEntryOfMap)
// ↑ Entry { keyElement: 123, valueElement: '123' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(19,27,32)
const largestNumberOfArray2 = await koconutArray2
.maxByOrNull(async eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray2)
// ↑ 32
const largest1sDigitNumberOfArray2 = await koconutArray2
.maxByOrNull(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(largest1sDigitNumberOfArray2)
// ↑ 19
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the largest value among all values produced by selector
function applied to each element in the collection or
throws KoconutNoSuchElementException if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,7,9)
const largestRemainderNumberDividedBy5OfArray = await koconutArray
.maxOf(eachNumber => eachNumber % 5)
.yield()
console.log(largestRemainderNumberDividedBy5OfArray)
// ↑ 4
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxOf(eachNumber => eachNumber % 5)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const longestStringLengthOfSet = await koconutSet
.maxOf(eachString => eachString.length)
.yield()
console.log(longestStringLengthOfSet)
// ↑ 3
class ComparableString implements KoconutComparable{
str : string
constructor(str : string) {
this.str = str
}
// Override
compareTo(other : ComparableString) : number {
return this.str.length - other.str.length
}
}
const maxComparableString = await koconutSet
.maxOf(eachString => new ComparableString(eachString))
.yield()
console.log(maxComparableString)
// ↑ ComparableString { str: 'abc' }
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthOfMap = await koconutMap
.maxOf(eachEntry => eachEntry.key)
.yield()
console.log(longestStringLengthOfMap)
// ↑ 3
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxOf(async eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitOfArray2 = await koconutArray2
.maxOf(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(largest1sDigitOfArray2)
// ↑ 5
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the largest value among all values produced by selector
function applied to each element in the collection or
null if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,7,9)
const largestRemainderNumberDividedBy5OfArray = await koconutArray
.maxOfOrNull(eachNumber => eachNumber % 5)
.yield()
console.log(largestRemainderNumberDividedBy5OfArray)
// ↑ 4
const largestRemainderNumberDividedBy5OfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxOfOrNull(eachNumber => eachNumber % 5)
.yield()
console.log(largestRemainderNumberDividedBy5OfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const longestStringLengthOfSet = await koconutSet
.maxOfOrNull(eachString => eachString.length)
.yield()
console.log(longestStringLengthOfSet)
// ↑ 3
class ComparableString implements KoconutComparable{
str : string
constructor(str : string) {
this.str = str
}
// Override
compareTo(other : ComparableString) : number {
return this.str.length - other.str.length
}
}
const maxComparableString = await koconutSet
.maxOfOrNull(eachString => new ComparableString(eachString))
.yield()
console.log(maxComparableString)
// ↑ ComparableString { str: 'abc' }
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthOfMap = await koconutMap
.maxOfOrNull(eachEntry => eachEntry.key)
.yield()
console.log(longestStringLengthOfMap)
// ↑ 3
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxOfOrNull(async eachNumber => eachNumber)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitOfArray2 = await koconutArray2
.maxOfOrNull(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(largest1sDigitOfArray2)
// ↑ 5
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the largest value according to the provided comparator
among all values
produced by selector
function applied to each element in the collection all throws KoconutNoSuchElementException
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of("1", "2", "3", "4", "5")
const largestNumberedStringOfArray = await koconutArray
.maxOfWith(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(largestNumberedStringOfArray)
// ↑ 5
try {
await koconutArray
.filter(eachString => eachString.length > 2)
.maxOfWith(
parseInt,
(front, rear) => front - rear
)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No string in "1" to "5" is logner than 2.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const lognestStringLengthOfSet = await koconutSet
.maxOfWith(
eachString => eachString.length,
(front, rear) => front - rear
)
.yield()
console.log(lognestStringLengthOfSet)
// ↑ 3
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthOfMap = await koconutMap
.maxOfWith(
eachEntry => eachEntry.key,
(front, rear) => front - rear
)
.yield()
console.log(longestStringLengthOfMap)
// ↑ 3
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxOfWith(
async eachNumber => eachNumber,
async (front, rear) => front - rear
)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitOfArray2 = await koconutArray2
.maxOfWith(
(eachNumber) => new Promise<number>(resolve => {
resolve(eachNumber % 10)
}),
(front, rear) => new Promise(resolve => {
resolve(front - rear)
})
)
.yield()
console.log(largest1sDigitOfArray2)
// ↑ 5
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the largest value according to the provided comparator
among all values
produced by selector
function applied to each element in the collection or null
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of("1", "2", "3", "4", "5")
const largestNumberedStringOfArray = await koconutArray
.maxOfWithOrNull(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(largestNumberedStringOfArray)
// ↑ 5
const largestNumberedStringOfEmptyArray = await koconutArray
.filter(eachString => eachString.length > 2)
.maxOfWithOrNull(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(largestNumberedStringOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const lognestStringLengthOfSet = await koconutSet
.maxOfWithOrNull(
eachString => eachString.length,
(front, rear) => front - rear
)
.yield()
console.log(lognestStringLengthOfSet)
// ↑ 3
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthOfMap = await koconutMap
.maxOfWithOrNull(
eachEntry => eachEntry.key,
(front, rear) => front - rear
)
.yield()
console.log(longestStringLengthOfMap)
// ↑ 3
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxOfWithOrNull(
async eachNumber => eachNumber,
async (front, rear) => front - rear
)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitOfArray2 = await koconutArray2
.maxOfWithOrNull(
(eachNumber) => new Promise<number>(resolve => {
resolve(eachNumber % 10)
}),
(front, rear) => new Promise(resolve => {
resolve(front - rear)
})
)
.yield()
console.log(largest1sDigitOfArray2)
// ↑ 5
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element having the largest value according to the provided comparator
or throws KoconutNoSuchElementException
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const largestNumberOfArray = await koconutArray
.maxWith((front, rear) => front - rear)
.yield()
console.log(largestNumberOfArray)
// ↑ 5
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxWith((front, rear) => front - rear)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc", "abcd")
const longestStringLengthOfSet = await koconutSet
.maxWith((front, rear) => front.length - rear.length)
.yield()
console.log(longestStringLengthOfSet)
// ↑ abcd
// Case 3
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthEntryOfMap = await koconutMap
.maxWith((front, rear) => front.key - rear.key)
.yield()
console.log(longestStringLengthEntryOfMap)
// ↑ Entry { keyElement: 3, valueElement: 'abc' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxWith(async (front, rear) => front - rear)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitNumberOfArray2 = await koconutArray2
.maxWith((front, rear) => new Promise(resolve => {
resolve(front % 10 - rear % 10)
}))
.yield()
console.log(largest1sDigitNumberOfArray2)
// ↑ 45
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element having the largest value according to the provided comparator
or null
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const largestNumberOfArray = await koconutArray
.maxWithOrNull((front, rear) => front - rear)
.yield()
console.log(largestNumberOfArray)
// ↑ 5
const largestNumberOfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.maxWithOrNull((front, rear) => front - rear)
.yield()
console.log(largestNumberOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc", "abcd")
const longestStringLengthOfSet = await koconutSet
.maxWithOrNull((front, rear) => front.length - rear.length)
.yield()
console.log(longestStringLengthOfSet)
// ↑ abcd
// Case 3
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const longestStringLengthEntryOfMap = await koconutMap
.maxWithOrNull((front, rear) => front.key - rear.key)
.yield()
console.log(longestStringLengthEntryOfMap)
// ↑ Entry { keyElement: 3, valueElement: 'abc' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const largestNumberOfArray2 = await koconutArray2
.maxWithOrNull(async (front, rear) => front - rear)
.yield()
console.log(largestNumberOfArray2)
// ↑ 51
const largest1sDigitNumberOfArray2 = await koconutArray2
.maxWithOrNull((front, rear) => new Promise(resolve => {
resolve(front % 10 - rear % 10)
}))
.yield()
console.log(largest1sDigitNumberOfArray2)
// ↑ 45
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element yielding the smallest value of the given function or throws KoconutNoSuchElementException if there are no elements.
1.0.10
Use minByOrNull instead.
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const smallestNumberOfArray = await koconutArray
.minBy(eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray)
// ↑ 1
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.minBy(eachNumber => eachNumber)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringOfSet = await koconutSet
.minBy(eachString => eachString.length)
.yield()
console.log(shortestStringOfSet)
// ↑ a
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1, 12, 123)
.associateWith(eachNumber => eachNumber.toString())
const shortestDigitsEntryOfMap = await koconutMap
.minBy(eachEntry => eachEntry.value.length)
.yield()
console.log(shortestDigitsEntryOfMap)
// ↑ Entry { keyElement: 1, valueElement: '1' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(19,27,32)
const smallestNumberOfArray2 = await koconutArray2
.minBy(async eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 19
const smallest1sDigitNumberOfArray2 = await koconutArray2
.minBy(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(smallest1sDigitNumberOfArray2)
// ↑ 32
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element yielding the smallest value of the given function or null
if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const smallestNumberOfArray = await koconutArray
.minByOrNull(eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray)
// ↑ 1
const smallestNumberOfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.minByOrNull(eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringOfSet = await koconutSet
.minByOrNull(eachString => eachString.length)
.yield()
console.log(shortestStringOfSet)
// ↑ a
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1, 12, 123)
.associateWith(eachNumber => eachNumber.toString())
const shortestDigitsEntryOfMap = await koconutMap
.minByOrNull(eachEntry => eachEntry.value.length)
.yield()
console.log(shortestDigitsEntryOfMap)
// ↑ Entry { keyElement: 1, valueElement: '1' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(19,27,32)
const smallestNumberOfArray2 = await koconutArray2
.minByOrNull(async eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 19
const smallest1sDigitNumberOfArray2 = await koconutArray2
.minByOrNull(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(smallest1sDigitNumberOfArray2)
// ↑ 32
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the smallest value among all values produced by selector
function applied to each element in the collection or
throws KoconutNoSuchElementException if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,7,9)
const smallestRemainderNumberDividedBy5OfArray = await koconutArray
.minOf(eachNumber => eachNumber % 5)
.yield()
console.log(smallestRemainderNumberDividedBy5OfArray)
// ↑ 1
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.minOf(eachNumber => eachNumber % 5)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringLengthOfSet = await koconutSet
.minOf(eachString => eachString.length)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ 1
class ComparableString implements KoconutComparable{
str : string
constructor(str : string) {
this.str = str
}
// Override
compareTo(other : ComparableString) : number {
return this.str.length - other.str.length
}
}
const minComparableString = await koconutSet
.minOf(eachString => new ComparableString(eachString))
.yield()
console.log(minComparableString)
// ↑ ComparableString { str: 'a' }
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthOfMap = await koconutMap
.minOf(eachEntry => eachEntry.key)
.yield()
console.log(shortestStringLengthOfMap)
// ↑ 1
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minOf(async eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitOfArray2 = await koconutArray2
.minOf(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(smallest1sDigitOfArray2)
// ↑ 0
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the smallest value among all values produced by selector
function applied to each element in the collection or
null
if there are no elements.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,7,9)
const smallestRemainderNumberDividedBy5OfArray = await koconutArray
.minOfOrNull(eachNumber => eachNumber % 5)
.yield()
console.log(smallestRemainderNumberDividedBy5OfArray)
// ↑ 1
const smallestRemainderNumberDividedBy5OfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.minOfOrNull(eachNumber => eachNumber % 5)
.yield()
console.log(smallestRemainderNumberDividedBy5OfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringLengthOfSet = await koconutSet
.minOfOrNull(eachString => eachString.length)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ 1
class ComparableString implements KoconutComparable{
str : string
constructor(str : string) {
this.str = str
}
// Override
compareTo(other : ComparableString) : number {
return this.str.length - other.str.length
}
}
const minComparableString = await koconutSet
.minOfOrNull(eachString => new ComparableString(eachString))
.yield()
console.log(minComparableString)
// ↑ ComparableString { str: 'a' }
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthOfMap = await koconutMap
.minOfOrNull(eachEntry => eachEntry.key)
.yield()
console.log(shortestStringLengthOfMap)
// ↑ 1
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minOfOrNull(async eachNumber => eachNumber)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitOfArray2 = await koconutArray2
.minOfOrNull(eachNumber => new Promise(resolve => {
resolve(eachNumber % 10)
}))
.yield()
console.log(smallest1sDigitOfArray2)
// ↑ 0
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
Returns the smallest value according to the provided comparator
among all values
produced by selector
function applied to each element in the collection all throws KoconutNoSuchElementException
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of("1", "2", "3", "4", "5")
const smallestNumberedStringOfArray = await koconutArray
.minOfWith(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(smallestNumberedStringOfArray)
// ↑ 1
try {
await koconutArray
.filter(eachString => eachString.length > 2)
.minOfWith(
parseInt,
(front, rear) => front - rear
)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No string in "1" to "5" is logner than 2.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringLengthOfSet = await koconutSet
.minOfWith(
eachString => eachString.length,
(front, rear) => front - rear
)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ 1
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthOfMap = await koconutMap
.minOfWith(
eachEntry => eachEntry.key,
(front, rear) => front - rear
)
.yield()
console.log(shortestStringLengthOfMap)
// ↑ 1
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minOfWith(
async eachNumber => eachNumber,
async (front, rear) => front - rear
)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitOfArray2 = await koconutArray2
.minOfWith(
(eachNumber) => new Promise<number>(resolve => {
resolve(eachNumber % 10)
}),
(front, rear) => new Promise(resolve => {
resolve(front - rear)
})
)
.yield()
console.log(smallest1sDigitOfArray2)
// ↑ 0
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the smallest value according to the provided comparator
among all values
produced by selector
function applied to each element in the collection all null
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of("1", "2", "3", "4", "5")
const smallestNumberedStringOfArray = await koconutArray
.minOfWithOrNull(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(smallestNumberedStringOfArray)
// ↑ 1
const smallestNumberedStringOfEmptyArray = await koconutArray
.filter(eachString => eachString.length > 2)
.minOfWithOrNull(
parseInt,
(front, rear) => front - rear
)
.yield()
console.log(smallestNumberedStringOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc")
const shortestStringLengthOfSet = await koconutSet
.minOfWithOrNull(
eachString => eachString.length,
(front, rear) => front - rear
)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ 1
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthOfMap = await koconutMap
.minOfWithOrNull(
eachEntry => eachEntry.key,
(front, rear) => front - rear
)
.yield()
console.log(shortestStringLengthOfMap)
// ↑ 1
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minOfWithOrNull(
async eachNumber => eachNumber,
async (front, rear) => front - rear
)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitOfArray2 = await koconutArray2
.minOfWithOrNull(
(eachNumber) => new Promise<number>(resolve => {
resolve(eachNumber % 10)
}),
(front, rear) => new Promise(resolve => {
resolve(front - rear)
})
)
.yield()
console.log(smallest1sDigitOfArray2)
// ↑ 0
A callback function that accepts an argument. The method calls the selector
one time for each element in object.
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the selector
. If thisArg
is omitted, null
is used as the this
value.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element having the smallest value according to the provided comparator
or throws KoconutNoSuchElementException
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const smallestNumberOfArray = await koconutArray
.minWith((front, rear) => front - rear)
.yield()
console.log(smallestNumberOfArray)
// ↑ 1
try {
await koconutArray
.filter(eachNumber => eachNumber > 10)
.minWith((front, rear) => front - rear)
.yield()
} catch(error) {
console.log(error.name)
// ↑ Koconut No Such Element Exception
// i.e. -- Array is filtered.
// No element in 1 to 5 is greater than 10.
}
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc", "abcd")
const shortestStringLengthOfSet = await koconutSet
.minWith((front, rear) => front.length - rear.length)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ a
// Case 3
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthEntryOfMap = await koconutMap
.minWith((front, rear) => front.key - rear.key)
.yield()
console.log(shortestStringLengthEntryOfMap)
// ↑ Entry { keyElement: 1, valueElement: 'a' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minWith(async (front, rear) => front - rear)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitNumberOfArray2 = await koconutArray2
.minWith((front, rear) => new Promise(resolve => {
resolve(front % 10 - rear % 10)
}))
.yield()
console.log(smallest1sDigitNumberOfArray2)
// ↑ 50
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Returns the first element having the smallest value according to the provided comparator
or null
if elements are empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const smallestNumberOfArray = await koconutArray
.minWithOrNull((front, rear) => front - rear)
.yield()
console.log(smallestNumberOfArray)
// ↑ 1
const smallestNumberOfEmptyArray = await koconutArray
.filter(eachNumber => eachNumber > 10)
.minWithOrNull((front, rear) => front - rear)
.yield()
console.log(smallestNumberOfEmptyArray)
// ↑ null
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("a", "ab", "abc", "abcd")
const shortestStringLengthOfSet = await koconutSet
.minWithOrNull((front, rear) => front.length - rear.length)
.yield()
console.log(shortestStringLengthOfSet)
// ↑ a
// Case 3
const koconutMap = KoconutArray.of("a", "ab", "abc")
.associate(eachString => [eachString.length, eachString])
const shortestStringLengthEntryOfMap = await koconutMap
.minWithOrNull((front, rear) => front.key - rear.key)
.yield()
console.log(shortestStringLengthEntryOfMap)
// ↑ Entry { keyElement: 1, valueElement: 'a' }
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(12,51,32,45,50)
const smallestNumberOfArray2 = await koconutArray2
.minWithOrNull(async (front, rear) => front - rear)
.yield()
console.log(smallestNumberOfArray2)
// ↑ 12
const smallest1sDigitNumberOfArray2 = await koconutArray2
.minWithOrNull((front, rear) => new Promise(resolve => {
resolve(front % 10 - rear % 10)
}))
.yield()
console.log(smallest1sDigitNumberOfArray2)
// ↑ 50
A callback function that accepts two arguements. The method calls the comparator
to compare two selected values.
In case the result is larger than 0, front is bigger than rear, and if it's less than 0 judge vice versa.
An object to which the this
keyword can refer in the comparator
. If thisArg
is omitted, null
is used as the this
value.
Creates an KoconutArray instance that wraps original data.
1.0.13
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const arrToArr = await koconutArray
.asArray()
.yield()
console.log(arrToArr)
// ↑ [ 1, 2, 3, 4, 5 ]
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,1,2,2,3,3,4,4,5,5)
const setToArr = await koconutSet
.asArray()
.yield()
console.log(setToArr)
// ↑ [ 1, 2, 3, 4, 5 ]
// Case 3 -- KoconutFlow
const koconutFlow = KoconutFlow.ofSimple(1,2,3,4,5)
const flowToArr = await koconutFlow
.asArray()
.yield()
console.log(flowToArr)
// ↑
// [
// Entry { keyElement: 0, valueElement: 1 },
// Entry { keyElement: 1, valueElement: 2 },
// Entry { keyElement: 2, valueElement: 3 },
// Entry { keyElement: 3, valueElement: 4 },
// Entry { keyElement: 4, valueElement: 5 }
// ]
// Case 4 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3,4,5)
.associate(eachNumber => [eachNumber, eachNumber])
const mapToArr = await koconutMap
.asArray()
.yield()
console.log(mapToArr)
// ↑
// [
// Entry { keyElement: 1, valueElement: 1 },
// Entry { keyElement: 2, valueElement: 2 },
// Entry { keyElement: 3, valueElement: 3 },
// Entry { keyElement: 4, valueElement: 4 },
// Entry { keyElement: 5, valueElement: 5 }
// ]
Creates an KoconutSet instance that wraps original data.
1.0.13
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const arrToSet = await koconutArray
.asSet()
.yield()
console.log(arrToSet)
// ↑ Set { 1, 2, 3, 4, 5 }
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,1,2,2,3,3,4,4,5,5)
const setToSet = await koconutSet
.asSet()
.yield()
console.log(setToSet)
// ↑ Set { 1, 2, 3, 4, 5 }
// Case 3 -- KoconutFlow
const koconutFlow = KoconutFlow.ofSimple(1,2,3,4,5)
const flowToSet = await koconutFlow
.asSet()
.yield()
console.log(flowToSet)
// ↑
// Set {
// Entry { keyElement: 0, valueElement: 1 },
// Entry { keyElement: 1, valueElement: 2 },
// Entry { keyElement: 2, valueElement: 3 },
// Entry { keyElement: 3, valueElement: 4 },
// Entry { keyElement: 4, valueElement: 5 }
// }
// Case 4 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3,4,5)
.associate(eachNumber => [eachNumber, eachNumber])
const mapToSet = await koconutMap
.asSet()
.yield()
console.log(mapToSet)
// ↑
// Set {
// Entry { keyElement: 1, valueElement: 1 },
// Entry { keyElement: 2, valueElement: 2 },
// Entry { keyElement: 3, valueElement: 3 },
// Entry { keyElement: 4, valueElement: 4 },
// Entry { keyElement: 5, valueElement: 5 }
// }
Return true
if all elements match te given predicate
.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const areAllArrayElementsGreaterThan0 = await koconutArray
.all(eachNumber => eachNumber > 0)
.yield()
console.log(areAllArrayElementsGreaterThan0)
// ↑ true
const areAllArrayElementsEven = await koconutArray
.all(eachNumber => eachNumber % 2 == 0)
.yield()
console.log(areAllArrayElementsEven)
// ↑ false -- i.e. '1' is not an even number.
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const areAllSetElementsGreaterThan0 = await koconutSet
.all(eachNumber => eachNumber > 0)
.yield()
console.log(areAllSetElementsGreaterThan0)
// ↑ true
const areAllSetElementsOdd = await koconutSet
.all(eachNumber => eachNumber % 2 == 1)
.yield()
console.log(areAllSetElementsOdd)
// ↑ false -- i.e. '2' is not an odd number.
// Case 3 -- KoconutMap
const koconutMap = KoconutMap.of(
[0, 0],
[1, 1],
[2, 2]
)
const areAllMapEntriesKeyEqualsToValue = await koconutMap
.all(eachEntry => eachEntry.key == eachEntry.value)
.yield()
console.log(areAllMapEntriesKeyEqualsToValue)
// ↑ true
const areAllMapEntriesSumGreaterThan3 = await koconutMap
.all(eachEntry => eachEntry.key + eachEntry.value > 3)
.yield()
console.log(areAllMapEntriesSumGreaterThan3)
// ↑ false -- i.e. Sum of key and value of first Entry { 0, 0 } is 0.
// It's definetly less than 3
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(1,2,3,4,5)
const areAllArrayElementsLessThan10 = await koconutArray2
.all(async eachNumber => eachNumber < 10)
.yield()
console.log(areAllArrayElementsLessThan10)
// ↑ true
const areAllArrayElementsOdd = await koconutArray2
.all(eachNumber => new Promise(resolve => {
resolve(eachNumber % 2 == 1)
}))
.yield()
console.log(areAllArrayElementsOdd)
// ↑ false -- i.e. '2' is not an odd number.
A callback function that accepts an argument. The method calls the predicate
one time for each element in object.
An object to which the this
keyword can refer in the predicate
. If thisArg
is omitted, null
is used as the this
value.
Returns true
if the collection has at least one element matches the given predicate
.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const isAnyArrayElementGreaterThan4 = await koconutArray
.any(eachNumber => eachNumber > 4)
.yield()
console.log(isAnyArrayElementGreaterThan4)
// ↑ true -- i.e. '5' is greater than 4.
const isAnyArrayElementMultipleOf6 = await koconutArray
.any(eachNumber => eachNumber % 6 == 0)
.yield()
console.log(isAnyArrayElementMultipleOf6)
// ↑ false
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const isAnySetElementGreaterThan3 = await koconutSet
.any(eachNumber => eachNumber > 3)
.yield()
console.log(isAnySetElementGreaterThan3)
// ↑ true -- i.e. '4' is greater than 3.
const isAnySetElementLessThan0 = await koconutSet
.any(eachNumber => eachNumber < 0)
.yield()
console.log(isAnySetElementLessThan0)
// ↑ false
// Case 3 -- KoconutMap
const koconutMap = KoconutMap.of(
[0, 0],
[1, 1],
[2, 2]
)
const isAnyMapEntrySumGreaterThan3 = await koconutMap
.any(eachEntry => eachEntry.key + eachEntry.value > 3)
.yield()
console.log(isAnyMapEntrySumGreaterThan3)
// ↑ true -- i.e. Sum of key and value of third Entry { 2, 2 } is 4.
// It's grater than 4.
const isAnyMapEntryKeyMultipleOf4 = await koconutMap
.any(eachEntry => eachEntry.key > 0 && eachEntry.key % 4 == 0)
.yield()
console.log(isAnyMapEntryKeyMultipleOf4)
// ↑ false
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(1,2,3,4,5)
const isAnyArrayElementLessThan2 = await koconutArray2
.any(async eachNumber => eachNumber < 2)
.yield()
console.log(isAnyArrayElementLessThan2)
// ↑ true -- i.e. '1' is less than 2.
const isAnyArrayElementGreaterThan7 = await koconutArray2
.any(eachNumber => new Promise(resolve => {
resolve(eachNumber > 7)
}))
.yield()
console.log(isAnyArrayElementGreaterThan7)
// ↑ false
A callback function that accepts an argument. The method calls the predicate
one time for each element in object.
An object to which the this
keyword can refer in the predicate
. If thisArg
is omitted, null
is used as the this
value.
Returns true
if the collection is empty (contains no elements), false
otherwise.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const isNumberArrayEmpty = await koconutArray
.isEmpty()
.yield()
console.log(isNumberArrayEmpty)
// ↑ false
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const isFilteredNumberSetEmpty = await koconutSet
.filter(eachNumber => eachNumber > 10)
.isEmpty()
.yield()
console.log(isFilteredNumberSetEmpty)
// ↑ true
// Case 3 -- KoconutMap
const koconutMap = new KoconutMap<number, number>()
const isNumberPairedMapEmpty = await koconutMap
.isEmpty()
.yield()
console.log(isNumberPairedMapEmpty)
// ↑ true
Returns true
if the collection is not empty.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const isNumberArrayEmpty = await koconutArray
.isNotEmpty()
.yield()
console.log(isNumberArrayEmpty)
// ↑ true
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const isFilteredNumberSetEmpty = await koconutSet
.filter(eachNumber => eachNumber > 10)
.isNotEmpty()
.yield()
console.log(isFilteredNumberSetEmpty)
// ↑ false
// Case 3 -- KoconutMap
const koconutMap = new KoconutMap<number, number>()
const isNumberPairedMapEmpty = await koconutMap
.isNotEmpty()
.yield()
console.log(isNumberPairedMapEmpty)
// ↑ false
Returns true
if this nullable collection is either null or empty.
1.0.10
Use isEmpty instead.
1.3.0
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const isNumberArrayEmpty = await koconutArray
.isNullOrEmpty()
.yield()
console.log(isNumberArrayEmpty)
// ↑ false
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const isFilteredNumberSetEmpty = await koconutSet
.filter(eachNumber => eachNumber > 10)
.isNullOrEmpty()
.yield()
console.log(isFilteredNumberSetEmpty)
// ↑ true
// Case 3 -- KoconutMap
const koconutMap = new KoconutMap<number, number>()
const isNumberPairedMapEmpty = await koconutMap
.isNullOrEmpty()
.yield()
console.log(isNumberPairedMapEmpty)
// ↑ true
predicate
callback function is optional. If it's omitted the method returns true
if the collection has no elements.
Otherwise, returns true
if no elements match the given predicate
.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = new KoconutArray<number>()
const isNoneOfAnEmptyArray = await koconutArray
.none()
.yield()
console.log(isNoneOfAnEmptyArray)
// ↑ true
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const isNoneOfSetElementGreaterThan10 = await koconutSet
.none(eachNumber => eachNumber >= 10)
.yield()
console.log(isNoneOfSetElementGreaterThan10)
// ↑ true
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3,4,5)
.associateWith(eachNumber => eachNumber * 2)
const isNoneOfEntryOfMapHasLessThan3DifferenceBetweenKeyAndValue
= await koconutMap
.none(eachEntry => eachEntry.value - eachEntry.key <= 3)
.yield()
console.log(isNoneOfEntryOfMapHasLessThan3DifferenceBetweenKeyAndValue)
// ↑ false
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(1,2,3,4,5)
const isNoneOfArrayElementGreaterThan3 = await koconutArray2
.none(async eachNumber => eachNumber >= 3)
.yield()
console.log(isNoneOfArrayElementGreaterThan3)
// ↑ false
const isNoneOfArrayelementLessThan0 = await koconutArray2
.none(eachNumber => new Promise(resolve => {
resolve(eachNumber <= 0)
}))
.yield()
console.log(isNoneOfArrayelementLessThan0)
// ↑ true
A callback function that accepts an argument. The method calls the predicate
one time for each element in object.
An object to which the this
keyword can refer in the predicate
. If thisArg
is omitted, null
is used as the this
value.
Performs the given action
on each element.
When you want to stop iteration in the meantime return
false
or BREAK.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5,6,7)
await koconutArray
.forEach(console.log)
// ↑ 1 2 3 4 5 6 7 -- i.e. This will print out each number
.process()
await koconutArray
.forEach(eachNumber => {
if(eachNumber > 4) return KoconutLoopSignal.BREAK
console.log(eachNumber)
})
// ↑ 1 2 3 4 -- i.e. Since '5', it is greater than 4, so the loop is broken.
.process()
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,1,2,3)
await koconutSet
.forEach(console.log)
// ↑ 1 2 3 -- i.e. All conflicted numbers will be ignored.
.process()
await koconutSet
.forEach(eachNumber => {
if(eachNumber % 2 == 0) return false
console.log(eachNumber)
})
// ↑ 1 -- i.e. Since '2', it is an even number, so the loop is broken.
.process()
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3)
.associateWith(eachElement => eachElement)
await koconutMap
.forEach(console.log)
// ↑
// Entry { keyElement: 1, valueElement: 1 }
// Entry { keyElement: 2, valueElement: 2 }
// Entry { keyElement: 3, valueElement: 3 }
.process()
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(1,2,3)
await koconutArray2
.forEach(async eachNumber => console.log(eachNumber))
// ↑ 1 2 3
.process()
await koconutArray2
.forEach(eachNumber => new Promise(resolve => resolve(console.log(eachNumber))))
// ↑ 1 2 3
.process()
A callback function that accepts an argument. The method calls the action
one time for each element in object.
An object to which the this
keyword can refer in the action
. If thisArg
is omitted, null
is used as the this
value.
Processes all the chained objects and calls the specified function
block
with the result value as its argument and returns the original result.
1.0.10
import { KoconutArray } from 'koconut'
const mainProcess = async () => {
const koconutNumbers = KoconutArray.of(1,2,3,4,5)
const moreNumbers = await koconutNumbers
.also(result => {
result.push(6)
result.push(7)
result.push(8)
})
console.log(moreNumbers)
// ↑ [1, 2, 3, 4, 5, 6, 7, 8]
}
mainProcess()
A callback function that accepts an argument.
Processes all the chained objects and calls the specified function
block
with the result value as its argument and returns the final result
of the block
.
1.0.10
import { KoconutArray } from 'koconut'
const mainProcess = async () => {
const koconutNumbers = KoconutArray.of(1,2,3,4,5)
const firstNumberPlus2 = await koconutNumbers
.first()
.let(result => result + 2)
console.log(firstNumber)
// ↑ 3
}
mainProcess()
A callback function that accepts an argument. The method calls the block
and returns its result.
Processes all the chained objects ane returns Promise<void>
.
1.0.10
import { KoconutArray } from 'koconut'
const mainProcess = async () => {
const koconutNumbers = KoconutArray.of(1,2,3,4,5)
await koconutNumbers
.forEach(console.log)
.process()
// ↑ 1 2 3 4 5
}
mainProcess()
Processes all the chained objects and return the result.
1.0.10
import { KoconutArray } from 'koconut'
const mainProcess = async () => {
const koconutNumbers = KoconutArray.of(1,2,3,4,5)
const firstNumber = await koconutNumbers
.first()
.yield()
console.log(firstNumber)
// ↑ 1
}
mainProcess()
Returns a single list of all elements yielded from results of transform
function being invoked on each element of original collection.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of("123", "45")
const allNumberInArray = await koconutArray
.flatMap(eachString => eachString)
// ↑ The string itself can be used as Iterable<string>.
// If you want to make it clear, also possible to type
// as eachString => eachString.split('')
.map(parseInt)
.yield()
console.log(allNumberInArray)
// ↑ [ 1, 2, 3, 4, 5 ]
// Case 2 - KoconutSet
const koconutSet = KoconutSet.of("abc", "de")
const allCharactersInSet = await koconutSet
.flatMap(eachString => eachString)
.yield()
console.log(allCharactersInSet)
// ↑ [ 'a', 'b', 'c', 'd', 'e' ]
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3,4,5)
.associateWith(eachNumber => eachNumber * 2)
const allKeysAndValuesInMap = await koconutMap
.flatMap(eachEntry => [eachEntry.key, eachEntry.value])
.yield()
console.log(allKeysAndValuesInMap)
// ↑ [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(123, 987)
const allDigitsInArray = await koconutArray2
.flatMap(async eachNumber => {
const digits = new Array<number>()
while(eachNumber != 0) {
digits.unshift(eachNumber % 10)
eachNumber = Math.floor(eachNumber / 10)
}
return digits
})
.yield()
console.log(allDigitsInArray)
// ↑ [ 1, 2, 3, 9, 8, 7 ]
const allNumberCharactersInArray = await koconutArray2
.flatMap(eachNumber => new Promise<string>(resolve => {
resolve(eachNumber.toString())
}))
.yield()
console.log(allNumberCharactersInArray)
// ↑ [ '1', '2', '3', '9', '8', '7' ]
A callback function that accepts an argument. The method calls the transform
one time for each element in object.
An object to which the this
keyword can refer in the transform
. If thisArg
is omitted, null
is used as the this
value.
Returns a list containing the results of applying the given transform
function
to each element in the original collection.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const doubledNumbersInArray = await koconutArray
.map(eachNumber => eachNumber * 2)
.yield()
console.log(doubledNumbersInArray)
// ↑ [ 2, 4, 6, 8, 10 ]
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of(1,2,3,4,5)
const doubledNumbersInSet = await koconutSet
.map(eachNumber => eachNumber * 2)
.yield()
console.log(doubledNumbersInSet)
// ↑ [ 2, 4, 6, 8, 10 ]
// Case 3 -- KoconutMap
const koconutMap = KoconutArray.of(1,2,3,4,5)
.associate(eachNumber => [eachNumber, eachNumber * 2])
const keyValueSumOfMap = await koconutMap
.map(eachEntry => eachEntry.key + eachEntry.value)
.yield()
console.log(keyValueSumOfMap)
// ↑ [ 3, 6, 9, 12, 15 ]
// Case 4 -- You can also do it asynchronously
const koconutArray2 = KoconutArray.of(1,2,3,4,5)
const squaredNumberInArray = await koconutArray2
.map(async eachNumber => eachNumber * eachNumber)
.yield()
console.log(squaredNumberInArray)
// ↑ [ 1, 4, 9, 16, 25 ]
const trippledNumbersInArray = await koconutArray2
.map(eachNumber => new Promise(resolve => {
resolve(eachNumber * 3)
}))
.yield()
console.log(trippledNumbersInArray)
// ↑ [ 3, 6, 9, 12, 15 ]
A callback function that accepts an argument. The method calls the transform
one time for each element in object.
An object to which the this
keyword can refer in the transform
. If thisArg
is omitted, null
is used as the this
value.
Returns a list containing results that are not null
nor undefined
of applying
the given transfrom
function to each element in the original collection. You can use this method as filter
then map
.
1.0.10
// Case 1 -- KoconutArray
const koconutArray = KoconutArray.of(1,2,3,4,5)
const squaredOddNumbersInArray = await koconutArray
.mapNotNull(eachNumber => {
if(eachNumber % 2 == 1)
return eachNumber * eachNumber
// return
// return null
// return undefined
// ↑ You can use any one of
// them or just omit it.
})
.yield()
console.log(squaredOddNumbersInArray)
// ↑ [ 1, 9, 25 ]
// Case 2 -- KoconutSet
const koconutSet = KoconutSet.of("1", "54", "26", "5")
const twoDigitsNumbersInSet = await koconutSet
.mapNotNull(eachString => {
if(eachString.length == 2)
return parseInt(eachString)
})
.yield()
console.log(twoDigitsNumbersInSet)
// ↑ [ 54, 26 ]
// Case 3 -- KoconutMap
const KoconutMap = KoconutArray.of(1,2,3,4,5)
.associate(eachNumber => [eachNumber, eachNumber * 2])
const oddKeyKeyValueSumInMap = await KoconutMap
.mapNotNull(eachEntry => {
if(eachEntry.key % 2 == 1)
return eachEntry.key + eachEntry.value
})
.yield()
console.log(oddKeyKeyValueSumInMap)
// ↑ [ 3, 9, 15 ]
// Case 4 -- You can also do it asynchronously
const koocnutArray2 = KoconutArray.of(1,2,3,4,5)
const squaredEvenNumbersInArray = await koocnutArray2
.mapNotNull(async eachNumber => {
if(eachNumber % 2 == 0)
return eachNumber * eachNumber
})
.yield()
console.log(squaredEvenNumbersInArray)
// ↑ [ 4, 16 ]
const doubledOddNumbersInArray = await koocnutArray2
.mapNotNull(eachNumber => new Promise<number | null>(resolve => {
if(eachNumber % 2 == 1)
resolve(eachNumber * 2)
else resolve(null)
}))
.yield()
console.log(doubledOddNumbersInArray)
// ↑ [ 2, 6, 10 ]
A callback function that accepts an argument. The method calls the transform
one time for each element in object.
An object to which the this
keyword can refer in the transform
. If thisArg
is omitted, null
is used as the this
value.
Generated using TypeDoc
Returns the number of the elements matching the given
predicate
. If thepredicate
is ommitted it'll returns the whole number of elements.Returns
Since
1.0.10
Example