Skip to content

Instantly share code, notes, and snippets.

View le-doude's full-sized avatar

Ed Pelosi le-doude

View GitHub Profile
// here is an example of bad implementation that breaks encapsulation AND is hard to extend
class MyService {
void doSomethingWith(MyInterface instance) {
if(instance instanceof FirstImpl) {
doSomething((FirstImpl) instance);
} else if (instance instanceof SecondImpl) {
doSomethingElse((SecondImpl) instance);
} else {
defaultBehaviour(instance);
}
const RESOLUTIONS = [
{text: 'year', short: 'y', get: (d) => d.getFullYear(), set: (d, v) => d.setFullYear(v) },
{text: 'month', short: 'm', get: (d) => d.getMonth(), set: (d, v) => d.setMonth(v) },
{text: 'day', short: 'd', get: (d) => d.getDate(), set: (d, v) => d.setDate(v) },
{text: 'hour', short: 'h', get: (d) => d.getHours(), set: (d, v) => d.setHours(v) },
{text: 'minute', short: 'mi', get: (d) => d.getMinutes(), set: (d, v) => d.setMinutes(v) }
];
class DateHelper {
timeAgoInWords(date1, {brief, skip, limit} = {}) {
@le-doude
le-doude / private.xml
Created January 18, 2017 02:48
Jap PC USB keyboard to mac - fix forward slash/yen/|/underscore swap
<?xml version="1.0"?>
<root>
<item>
<name>Underscore and backlash settings FILCO jp layout</name>
<identifier>private.filco_jp_backslash_underscore</identifier>
<autogen>
__KeyToKey__
KeyCode::JIS_UNDERSCORE, MODIFIERFLAG_EITHER_LEFT_OR_RIGHT_SHIFT,
KeyCode::JIS_UNDERSCORE
</autogen>
@le-doude
le-doude / BytesFun.sc
Created July 28, 2015 00:49
Fun with bytes in scala. O(n) converstions hex to byte and back
val key = "201507252204"
val sample = """c409c8b91c7910f220ee4981b8df77c4 330dc24548f60c7e00cdcffd971d9ff6 62aab227bf0763900cb3d02a782a1fbb 121a181b6b7a36bb9113c48ed074a8d8 d9bbc89c25018ae1a274b0e660425eb0 fbd42e509a39e767191e6b3b690acaa1 07f8e1f3ae09416e8db60431d1267772 5e571af000d64fd4be6f16e97eab2b7a 3b023dbc640f9e39f0c69a268dcf29b1 ebc6895e8d9ec5e93e57f90efa236d12 e6090053978596e5b632795374df33a7 4f71c3077ceef9e39a3e6088ff2500c8 b73d0d3f17823d88d17e86bbca9b89da 60ff75ac2c022bc916b012bc5065e3c9 b3ca5dba8cbc44facf48999063375750 75d998d66f97f728d6aa8065feefe9dc 6174ae7a779df9ac090c73a7c9ca6581 8c94f5007c74d05ed216531da5b2384e d5d5ca57105b3ff4bceda29131e4ae61 32028e032cdec447fcbf6a9334c07400 483a79c0d62a0dfdca17ae2fd92b9a1a 7189f6aea57ffc4465b401a23bfe090b 5dd473722844c62e98d3087d4007bc20 896aa7dadf5b8c9767b93454feb32096 fbe111d30f80cd323bb9697bd087ea73 39dff0311ea3653d2b3ac900d88e2b81 2864adb1ae7e42de41485a6975c1c9d6 b2a2135b7b0281e302d23a30b1cecc4c 2273a3e7b5c8e95ae389522559b67288 7d5b30a0fc680f2f320401f6f5
@le-doude
le-doude / MyClobType.java
Last active August 29, 2015 14:06
The base for BLOB of Json in Hibernate/JPA2.1
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.usertype.UserType;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.sql.PreparedStatement;
package maxProductOfThree;
import java.util.Arrays;
import static java.lang.Math.*;
public class Solution {
public int solution(int[] A) {
if (A != null && A.length > 3) {
@le-doude
le-doude / ManacherAlgorithm.java
Created June 19, 2014 06:49
Solving the problem: Find the longest palindrome in a random character array. Can also be used to find all possible palindromes in the same sequence. http://en.wikipedia.org/wiki/Longest_palindromic_substring
import java.util.Arrays;
/**
* Created by le-doude on 14/06/19.
* <p/>
* Did you know it is possible to find the longest palindrom in a char sequence in O(N) time.
* Here is the solution: Manacher's algorithm.
* http://en.wikipedia.org/wiki/Longest_palindromic_substring
*/
public class ManacherAlgorithm {
@le-doude
le-doude / Solution.java
Created June 19, 2014 05:10
My solution to the https://codility.com/demo/take-sample-test/perm_missing_elem problem. solved in 30ssec flat!
class Solution {
public int solution(int[] A) {
int r = 0;
for (int i = 1; i <= A.length + 1; i++) {
r = r ^ i;
}
for (int i = 0; i < A.length; i++) {
r = r ^ A[i];
}
return r;
/**
* Created by edouard on 14/06/18.
*/
class Solution {
public int solution(int[] A) {
int maxIndex = A.length - 1;
int[] leftSum = new int[A.length];
leftSum[0] = A[0];
for (int i = 1; i < A.length; i++) {
leftSum[i] = A[i] + leftSum[i - 1];
@le-doude
le-doude / utility_functions.sql
Last active June 14, 2016 11:57
some functions I find extremely useful when working with jsons in postgresql 9.3
CREATE OR REPLACE FUNCTION public.json_append(data JSON, insert_data JSON)
RETURNS JSON
LANGUAGE SQL
AS $$
SELECT
('{' || string_agg(to_json(key) || ':' || value, ',') || '}') :: JSON
FROM (
SELECT
*
FROM json_each(data)