Skip to content

Instantly share code, notes, and snippets.

View MuffinTheMan's full-sized avatar

Caleb MuffinTheMan

View GitHub Profile
@MuffinTheMan
MuffinTheMan / ObjectUtil.java
Last active September 21, 2021 18:24
Simple helper method(s) for Java.
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.
*
@MuffinTheMan
MuffinTheMan / AddToListWhileIterating.java
Last active October 24, 2022 13:42
Snippets of Java code
@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");
}
@MuffinTheMan
MuffinTheMan / remove_at.rb
Created July 14, 2017 05:54
Ruby monkey patch for a version of Array#delete_at that returns the array with the element at the given index removed rather than returning the removed element
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
@MuffinTheMan
MuffinTheMan / JavaSnippets.java
Last active June 10, 2017 01:19
Java snippets for reference
// 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);
@MuffinTheMan
MuffinTheMan / nth0_2.pl
Last active December 30, 2015 09:08
Prolog predicate to get an element from a 2-dimensional list at some (Row, Column).
% 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.
# 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"
@MuffinTheMan
MuffinTheMan / pal.hs
Created September 11, 2013 15:13
This may or may not be a correct program in Haskell (my compiler isn't working, so I can't test it). It's supposed to check if the given input is a palindrome.
pal input = if (input == reverse input) then True else False
@MuffinTheMan
MuffinTheMan / fibonacciList.io
Last active December 22, 2015 09:58
Check if a list contains a fibonacci sequence in Io.
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)