Tag: clojure

Tricking the JVM JIT into speed

On the Clojurians Slack, PEZ brought some delicious Fibonacci performance tuning from the “languages” repo. While I don’t think that benchmark is as useful as mesmerizing the moving circles are in the animated graphs, I had a few very confusing and interesting discoveries.

Step 1: watch Alex Miller’s talk about Clojure’s interop performance. Step 2: learn how to actually do all that stuff. Step 3: notice that recursion does things. Step 4: magic.


Untyped JS objects in ClojureScript

Working on a Chrome plugin I got to use ClojureScript in yet another new environment. After the usual browser setup, Node, Deno and Github Actions, this time it was the Chrome plugin system. While it was mostly a very smooth ride, I had a little trouble figuring out how to deal with a “Cannot infer target type in expression” warning.

It showed up while dealing with a HAR entry object passed in to my callback from devtools.network.onRequestFinished. The HAR object, while its shape is well defined, is not typed in a strict sense (though I guess there may be some type definition for it somewhere out there if I looked hard enough). That’s why when I tried to access its fields like (-> entry .-request .-url) the ClojureScript compiler (through Shadow CLJS) would complain about inferring the “target type” as above.


ClojureDartで自前でwidget作ろうとするときの注意点

やりたかったことは多言語対応の一環だった。flutter_localizationsの生成コードでBuildContextからAppLocalizationは取得できるが、それと別にOSの言語設定を監視したかった。そのために他のウィジェットを包むだけのものを用意して、ミドルウェア的な挙動でOSの言語設定をアプリのDBに保管したかった。でもなぜかそれを適応すると、今度はgo_routerのStatefulShellRouteを使った遷移が機能しなくなってしまった。


extend-protocolでClass/forName怒られるワケ

Clojureでプロトコルをよく使う。defprotocolで作ってextend-protocolで各種の型に実装すると、いろんな入力値にスムーズに対応できる。例えば暗号化周りで、ハッシュや署名の算出のためにバイト配列が必要な際、プロトコルを活かしていい感じのAPIが提供できる。

(defprotocol Byteish
  (->bytes ^bytes [input]))

(extend-protocol Byteish
  String
  (->bytes [input] (.getBytes input)))

Supercharged OAuth scopes with reitit

You might be familiar with OAuth scopes from for example the Github dialog for creating a new access token. You get to choose what the token is authorized to do: can the user manage repos? Leave reviews? Push commits? There are a ton of options. Similarly Mastodon has scopes such as “see favorites” or “post on your behalf.”

padlock on black metal fence

Server-side rendering a ClojureScript app with Deno

タイトルは英語のままなのは、日本語にしようとしても結局全部カタカナになるので放置。さてre-frameは俺的にClojureScriptでウェブのフロントエンド作るにあたって標準装備になっているが、ものによっては完全動的に生成されるReactのアプリよりも、検索エンジンなどでも絶対拾える静的HTMLを返した方がいいという場合もある。今回は流行り言葉をいっぱい使えるように、Denoを活かしてClojureScriptアプリのサーバーサイドレンダリングを実現しようと思います。動くコードはGithubにて


Frontend/backend shared routing with reitit

Commonly frontend and backend are separate beasts. Backend written in Ruby using Rails for example, its routing written in its own DSL. Frontend written in TypeScript using Vue.js for example, its routing written in its own DSL. Of course the frontend will call some backend endpoints, so it should definitely know about those backend endpoints too, while there may be some frontend “paths” that don’t correspond to any single API endpoint, yet you might want to generate absolute URLs for those pages on the backend. This results in a nasty mess and duplication of routing and adjacent logic.

asphalt road between trees

Dealing with circular dependencies in Clojure

While working on stuff (of course in Clojure), I kept running into problems with namespaces having circular dependencies. For example I’d have an app.router namespace that defines the, uh, routes. I’d have an app.url namespace that contains helper functions to generate absolute URLs based on route data (so it depends on app.router). I’d have an app.views namespace that uses those URL helpers (thus depending on app.url), and these views would be referenced in the routes so app.router would require app.views.

This completes the dependency circle and is the beginning of my journey. There are a bunch of ways to deal with circular dependencies in Clojure, but I won’t go in-depth about all of them.

timelapse photography of fire

ClojureDartの第一印象

Clojureの文法でFlutterが書けると聞いた時、これだ!と思った。ClojureDartはただClojureの文法をDartに変換するツールよりも、Flutterがメインだと感じた(個人的にDartもそうだと思っている)。ClojureDartはすごく短時間で「やるぞ」の発表から実際にだれでも試せるような形に発展した。あとからコミットログ見返すと、もっと前から作っていたらしいけど。

最近モバイルアプリ手掛けることになって、プロトタイプ作るにClojureDart試すことにした。困ったことはいろいろとあったが、後悔はしていない。

black and brown dart board

Clojure multimethods and derivation

I’ve known about Clojure multimethods of course, but I never really used them much. I didn’t really have data that I’d need polymorphism like multimethods to handle, and when I did need something like that I’d use protocols. However protocols dispatch based on class, so when I faced the problem of handling ActivityPub objects that are all maps, it was time for defmulti to save the day.

red yellow and green abstract painting