Skip to content

Instantly share code, notes, and snippets.

Avatar

Moligaloo Moligaloo

  • ByteDance
  • Shanghai, China
View GitHub Profile
@Moligaloo
Moligaloo / Main.hx
Created Jan 25, 2021
Find square root by dichotomy and newton iteration
View Main.hx
class Main {
public static function main() {
trace(dichotomySqrt(2));
trace(newtonSqrt(2));
}
static function dichotomySqrt(x:Float):Float {
if (x < 0) {
return Math.NaN;
}
@Moligaloo
Moligaloo / AlwaysAllowSimulatorAccessNetwork.sh
Created Apr 27, 2020
always allow simulator process to access network, avoid annoying network permission request
View AlwaysAllowSimulatorAccessNetwork.sh
#!/bin/bash
# Script to disable the iOS Simulator app from showing the "Do you want the application xxxx to accept incoming network connections?" pop-up every time the app is run
echo "> Enter password to temporarily shut firewall off"
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off
echo "> Add Xcode as a firewall exception"
/usr/libexec/ApplicationFirewall/socketfilterfw --add /Applications/Xcode.app/Contents/MacOS/Xcode
@Moligaloo
Moligaloo / Block+asJson.io
Created May 22, 2018
Compile block to AST using JSON format
View Block+asJson.io
#!/usr/bin/env io
Yajl
Regex
Message do(
asMap := method(
if(
self name allMatchesOfRegex("(^[0-9\"])|true|false|nil") isNotEmpty,
@Moligaloo
Moligaloo / CFunction.io
Last active May 7, 2018
Directly parse C function declaration
View CFunction.io
#!/usr/bin/env io
CFunction := Object clone do(
returnType ::= nil
name ::= nil
paramTypes ::= nil
curlyBrackets := method(
msg_list := list()
msg := call argAt(0)
@Moligaloo
Moligaloo / MemberListFromCStruct.io
Created May 5, 2018
get member list from struct definition using C style DSL
View MemberListFromCStruct.io
#!/usr/bin/env io
StructDefinition := Object clone do(
name ::= ""
memberList ::= list()
with := method(name,
self clone setName(name) setMemberList(list())
)
@Moligaloo
Moligaloo / combine-video-clips.m
Last active May 11, 2016
Combine video clips in iOS using AVFoundation
View combine-video-clips.m
-(void) composeVideoClips:(NSMutableArray<AVURLAsset *> *)videoClips
forComposition:(AVMutableComposition *)composition
mediaType:(NSString *)mediaType
{
AVMutableCompositionTrack * composedTrack =
[composition addMutableTrackWithMediaType:mediaType
preferredTrackID:kCMPersistentTrackID_Invalid];
CMTime time = kCMTimeZero;
for (AVURLAsset *videoClip in videoClips) {
@Moligaloo
Moligaloo / launchscreen.rb
Created Apr 27, 2016
Generate iOS launch screen images
View launchscreen.rb
#!/usr/bin/env ruby
require 'rmagick'
if ARGV.length < 2
puts "Usage: #{$PROGRAM_NAME} <logo path> <background color>"
exit
end
logopath = ARGV[0]
View disgusting-ancient-style.rb
#!/usr/bin/env ruby
@two_chars_words = %w"朱砂 天下 杀伐 人家 韶华 风华 繁华 血染 墨染 白衣 素衣 嫁衣 倾城 孤城 空城 旧城 旧人 伊人 心疼 春风 古琴 无情 迷离 奈何 断弦 焚尽 散乱 陌路 乱世 笑靥 浅笑 明眸 轻叹 烟火 一生 三生 浮生 桃花 梨花 落花 烟花 离殇 情殇 爱殇 剑殇 灼伤 仓皇 匆忙 陌上 清商 焚香 墨香 微凉 断肠 痴狂 凄凉 黄梁 未央 成双 无恙 虚妄 凝霜 洛阳 长安 江南 忘川 千年 纸伞 烟雨 回眸 公子 红尘 红颜 红衣 红豆 红线 青丝 青史 青冢 白发 白首 白骨 黄土 黄泉 碧落 紫陌"
@four_chars_words = %w"情深缘浅 情深不寿 莫失莫忘 阴阳相隔 如花美眷 似水流年 眉目如画 曲终人散 繁华落尽 不诉离殇 一世长安"
@sentence_model = %w"xx,xx,xx了xx。 xxxx,xxxx,不过是一场xxxx。 你说xxxx,我说xxxx,最后不过xxxx。 xx,xx,许我一场xxxx。 一x一x一xx,半x半x半xx。 你说xxxxxxxx,后来xxxxxxxx。 xxxx,xxxx,终不敌xxxx。"
def get_sentence
model = @sentence_model.sample(1)[0].clone
while model.include?'xxxx'
model.sub!(/xxxx/, @four_chars_words.sample(1)[0])
@Moligaloo
Moligaloo / c-chain-invocation.c
Last active Jan 31, 2016
Chain invocation in C programming language
View c-chain-invocation.c
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
// MyJIT homepage: http://myjit.sourceforge.net/index.htm
#include "myjit/jitlib.h"
struct string_builder{
GString *gstring;
@Moligaloo
Moligaloo / HelpfulBreakpoints.m
Created Jan 9, 2016
help iOS developer to breakpoint when NSDictionary construction failed because of nil object
View HelpfulBreakpoints.m
#import <objc/runtime.h>
#import <objc/message.h>
// copied from http://stackoverflow.com/questions/17346046/advice-on-how-to-catch-attempt-to-insert-nil-object-from-a-device-needed
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
static id safe_initWithObjects(id self, SEL _cmd, const id objects[], const id <NSCopying> keys[], NSUInteger count) {
id orignialResult = nil;