Skip to content

Instantly share code, notes, and snippets.

@josephok
josephok / swap_array.scala
Created April 6, 2016 10:09
scala for the impatient excercises chapter 3 excercise 2
def swap_array(a: Array[Int]) = {
for (i <- 0 until a.length) {
if ((i % 2 == 0) && (i+1) < a.length) {
val t = a(i)
a(i) = a(i+1)
a(i+1) = t
}
}
a
}
@josephok
josephok / token.coffee
Created April 1, 2016 10:05
Keystone token-get use username and password
#!/usr/bin/env coffee
request = require "request"
argv = require('yargs')
.option('u', {
alias : 'url',
demand: true,
describe: 'Keystone auth url, just specify controller ip address',
type: 'string'
# Your init script
#
# Atom will evaluate this file each time a new window is opened. It is run
# after packages are loaded/activated and after the previous editor state
# has been restored.
#
# An example hack to log to the console when each text editor is saved.
#
# atom.workspace.observeTextEditors (editor) ->
# editor.onDidSave ->
@josephok
josephok / top10.sh
Created August 11, 2015 07:38
Get top 10 memory-consuming applications
ps -aux | sort -nr -k 6 | head -10 | awk '{printf "%dM %s\n", $6/1024, $11}'
@josephok
josephok / commandlinefu.coffee
Created July 6, 2015 09:21
acquire a list of commands via http://www.commandlinefu.com/ API
#!/usr/bin/env coffee
request = require "request"
require "colors"
URL = "http://www.commandlinefu.com/commands/browse/sort-by-votes/json"
request URL, (error, response, body) ->
if error
console.log("Got error: " + error.message)
@josephok
josephok / iplocation.sh
Last active August 29, 2015 14:24
Get my ip location
#!/bin/bash
ipsource=$(curl -s www.123cha.com | grep "您的ip")
ip=$(echo $ipsource | grep -oP "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" | head -1)
location=$(echo $ipsource | grep -o "来自:&nbsp;.*++" | sed -e 's/来自:&nbsp;//g' -e 's/&nbsp;/-/g' -e 's/-++//g')
echo "$ip $location"
@josephok
josephok / quicksort.js
Created May 13, 2015 10:34
quick sort in Javascript
var quickSort = function(arr) {
if (!Array.isArray(arr)) throw new Error("arguments should be an array");
  if (arr.length <= 1) { return arr; }
  var pivotIndex = Math.floor(arr.length / 2);
  var pivot = arr.splice(pivotIndex, 1)[0];
  var left = [];
  var right = [];
  for (var i = 0; i < arr.length; i++){
    if (arr[i] < pivot) {
      left.push(arr[i]);
@josephok
josephok / epoll_test1.py
Last active August 29, 2015 14:15
epoll test 1
import socket, select
EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'
response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'
response += b'Hello, world!'
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@josephok
josephok / node_request.coffee
Last active August 29, 2015 14:14
requests test
fs = require "fs"
request = require "request"
URLS = ("http://www.fanjian.net/page/#{i}" for i in [1..50])
for url in URLS
pos = url.lastIndexOf "/"
n = url.slice pos + 1
console.log n
request(url).pipe(fs.createWriteStream("output/#{n}.html"))
@josephok
josephok / client.c
Last active September 6, 2018 08:51
a stream socket server & client demo
/*
** client.c -- a stream socket client demo
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>