Skip to content

Instantly share code, notes, and snippets.

View fehmicansaglam's full-sized avatar
🚶‍♂️
Wandering

Fehmi Can Sağlam fehmicansaglam

🚶‍♂️
Wandering
View GitHub Profile
@fehmicansaglam
fehmicansaglam / redis_count_all_keys_matching_pattern
Created November 1, 2015 19:10
Count all keys matching a pattern on server
eval "return #redis.pcall('keys', 'prefix*')" 0
@fehmicansaglam
fehmicansaglam / 0_reuse_code.js
Created November 1, 2015 19:08
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
package net.fehmicansaglam.minheap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unused")
public class MinHeap<K, V extends Comparable<V>, T extends MinHeap.Item<K, V>> {
@fehmicansaglam
fehmicansaglam / Search.scala
Created April 28, 2015 10:48
How to search for an element in an array while passing state for the next iteration
val numbers = readLine().split(" ").map(_.toInt) // an array of numbers
val leftSums = numbers.scan(0)(_ + _).init // calculate cumulative sums from left
// search from right to find the same sum from left
var sum = 0
val result = numbers.zipWithIndex.reverse.find { case (number, index) =>
if (sum == leftSums(index)) true
else {
sum = sum + number
false
@fehmicansaglam
fehmicansaglam / 1_Good parts
Last active August 29, 2015 14:00
Why I think scalatutorials.com uses the wrong way to teach Scala
Step by step tutorial, the UX, and the worksheet are great.
(Although the worksheet did not run for some of the lessons on my Chrome, Ubuntu 14.04)
@fehmicansaglam
fehmicansaglam / Bilye.scala
Created January 13, 2013 22:32
Bilye probleminin Scala ile çözümü
object Bilye extends App {
val onbellek = collection.mutable.Map[(Int, Int), Boolean]()
def onbellekliSpragueGrundy(p: Int, q: Int): Boolean = {
onbellek.getOrElseUpdate((p, q), spragueGrundy(p, q))
}
def spragueGrundy(p: Int, q: Int) = {
if (p == 0) true
@fehmicansaglam
fehmicansaglam / Exception.java
Created November 12, 2012 09:27
Catch Interceptor
@Catch
static void exception(Throwable t) {
// Hatayı logla
Logger.error(ExceptionUtils.getStackTrace(t));
// Dönüş mesajını oluştur
Throwable cause = t.getCause();
String message = null;
if (cause != null && cause instanceof ConstraintViolationException) {
message = "constraint violation";
@fehmicansaglam
fehmicansaglam / Application.java
Created November 3, 2012 09:49
Play Custom Json Result
package controllers;
import static controllers.result.CustomJsonResult.renderCustomJson;
import java.util.Arrays;
import java.util.List;
import play.mvc.Controller;
public class Application extends Controller {
@fehmicansaglam
fehmicansaglam / ApiClient.java
Created June 2, 2012 12:48
AsyncStreamHandler
public static ListenableFuture<Response> readFile(final String documentUuid,
final OutputStream os) {
final String requestUrl = ...;
try {
return new AsyncHttpClient().prepareGet(requestUrl)
.addQueryParameter("documentUuid", documentUuid)
.execute(new AsyncStreamHandler(os, documentUuid));
} catch (final IOException e) {
throw new UnexpectedException(e);
@fehmicansaglam
fehmicansaglam / JsonFormat.java
Created May 8, 2012 14:12
Pretty-Print JSON in Java
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);