This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.coolstuffs.util; | |
import javax.annotation.Nullable; | |
public class ObjectUtil { | |
/** | |
* Similar to MySQL IFNULL(). If first parameter is null, second parameter will be | |
* returned--otherwise, first parameter will be returned. Think of this as the first | |
* parameter being the preferred value and the second parameter as the fall-back value. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
void standardForLoop() { | |
final List<String> fruit = new ArrayList<>( | |
Arrays.asList("apple", "banana", "orange", "mango") | |
); | |
int counter = 0; | |
for (int i = 0; i < fruit.size(); i++) { | |
if (counter++ < 10) { | |
fruit.add("pineapple"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def remove_at(i) | |
# handle index out of bounds by returning unchanged array | |
return self if i >= self.length | |
# remove the i-th element from the end if i is negative | |
if i < 0 | |
i += self.length | |
# handle index out of bounds by returning unchanged array | |
return self if i < 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Two ways to "for each / foreach" | |
String letters = "abcdefghijklmnopqrstuvwxyz"; | |
char[] letterArr = letters.toCharArray(); | |
for (int i = 0; i < letterArr.length; i++) { | |
System.out.println(letterArr[i]); | |
} | |
// Above and below are the same in result | |
for (char c : letterArr) { | |
System.out.println(c); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Get an element from a 2-dimensional list at (Row,Column) | |
% using 0-based indexing. | |
nth0_2(Row, Column, List, Element) :- | |
nth0(Row, List, SubList), | |
nth0(Column, SubList, Element). | |
% Example use: | |
% | |
% ?- nth0_2(2, 1, [[1,0], [2,9], [3,4]], El). | |
% El = 4. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save as ".profile" in home directory | |
# Source with `source ~/.profile` | |
alias ..="cd .." | |
alias ...="cd ../.." | |
alias ....="cd ../../.." | |
alias .....="cd ../../../.." | |
alias la="ls -a" | |
alias ll="ls -l" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pal input = if (input == reverse input) then True else False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibonacci := method(num, | |
if (num < 0, return "oops...negative number") | |
if (num == 0, return 0) | |
if (num == 1, return 1) | |
if (num == 2, return 1) | |
return fibonacci(num - 1) + fibonacci(num - 2) | |
) | |
isFibonacci := method(num, | |
if (num < 0, return false) |