2022-12-15
Useful Java Patterns

java stream guide

learn java and kotlin the same time?

beanshell syntax highlight seems to be rare. beanshell workspace has the highlight.

use v2.1.1 and above for varargs support.

there are eclipse and jetbrains plugin support for beanshell.

creating lists

1
2
3
4
5
6
7
8
var a = new ArrayList<>(Arrays.asList(1,2,3));
var mset = new HashSet<>();
mset.addAll(a);
var mset2 = a.stream().collect(Collectors.toSet());
var mymin = Collections.min(a);
var mymax = Collections.max(a);
Collections.reverse(a);

1
2
3
4
5
6
var a = arrayOf(1,2,3);
print(a.contentToString())
print(a.max())
print(a.min())
a.reverse()

creating hashmap

1
2
3
var ah= new HashMap<>();
ah.put(1,2);

1
2
var ah = hashMapOf(1 to 2, 2 to 3)

iterate lists with indices

java double colon :: operator acts as anonymous function

1
2
3
4
5
6
7
8
9
10
11
12
var l = a.listIterator();
while (l.hasNext()){
var index = l.nextIndex();
var val = l.next();
System.out.println("INDEX: "+index+" ELEM: "+val);
}
IntStream.range(0,a.size()).forEach(index -> a.get(index));
var index=0;
for (int i: a){
index++;
}

1
2
3
4
5
6
7
8
9
a.forEachIndexed{ind, elem -> println("index? $ind"); println("elem? $elem")}
for (var i in a.indices){
var elem = a[i]
}
a.indices.forEach {
var elem = a[it]
elem
}

list comprehension

1
2
3
4
var mlist = a.stream().map(x-> x*2).collect(Collectors.toList());
var evenNums = a.stream().filter(x-> x%2 == 0).collect(Collectors.toList());
var mmap = a.stream().collect(Collectors.toMap(x->x.getId(),x->x.getName()));

switch expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var val = 2;
var mswitch = switch (val){
case 1,2,3 -> {
yield "good";
}
case 4,5,6 -> {
yield "bad";
} // either throw or yield.
default -> {
System.out.println("out of expectation");
yield "really bad";
}
};
System.out.println(mswitch);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var grade = 30
var res = when(grade) {
in 0..40 -> "Fail"
in 41..70 -> "Pass"
in 71..100 -> "Distinction"
else -> false
}
print(res)
var mcase = 1
var res = when(mcase){
1 -> "good"
2 -> "bad"
else -> "really bad"
}
print(res)

count occurance of elements in array

1
2
3
4
5
6
7
8
9
10
11
12
String[] array = {"name1","name2","name3","name4", "name5", "name2"};
Arrays.stream(array)
.collect(Collectors.groupingBy(s -> s))
.forEach((k, v) -> System.out.println(k+" "+v.size()));
List asList = Arrays.asList(array);
Set<String> mySet = new HashSet<String>(asList);
for(String s: mySet){
System.out.println(s + " " + Collections.frequency(asList,s));
}
Map<String, Long> map = Arrays.stream(array)
.collect(Collectors.groupingBy(s -> s, Collectors.counting()));

1
2
3
var a = arrayOf(1,2,3,3,3,3)
a.toSet().forEach{it -> println("elem? $it"); println("count? "+a.count{it2->it2 == it})}

lambdas

1
2
3
4
5
6
Consumer mcons = (n) -> {System.out.println(n);}
mcons.andThen(mcons).accept("mval");
Function <Integer,Integer> mfunc = n-> n+1;
Supplier msup = () -> 1;
var mval = msup.get();

1
2
3
var mfunc = {n :Int -> n+1}
var mprint = {n:Any -> print(n)}

Read More

2022-11-25
Hy Programming Language Enhancements For Automation And Ide Support

hy lisp embedded in python, resumeable exception

jython only supports upto python2.7. these libraries may need substantial changes to be compatible with jython:

1
2
3
4
5
hy
parse
hyrule
reloading

hy docs

hyrule docs

hy缺乏基本的补全 vscode目前没法用 我把hy改造成了可以自动reload 自动抓Uncaught exception的模式 如果要取消这些行为 需要加上flag 可以用于hy2py

1
2
3
4
5
6
7
8
9
-R
disable automatic insertion of reloading decorator
-T
disable toplevel try-except
-K
disable toplevel show stacktrace
-L
disable line-by-line try-except

一些未知的expression可能不允许被wrap到我们的macro里面 需要被加入到黑名单 已知的系列包括unpack-iterable之类的

hy的插件目前分为vim版本和EMACS版本 对spacevim spacemacs不怎么友好

vim syntax highlight

emacs hy-mode and jedhy

hyuga for neovim, with custom vim-lsp

hy的code autoindentation功能我做了 nelean 目前最新版本有待更新 主要是对字符串的正则进行了优化

Read More

2022-11-11
Call Ruby From Python

Read More

2022-07-13
Repl For Assembly Code

with processor flags output

https://github.com/yrp604/rappel

msf provided repl

msf-nasm_shell

Read More