Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure to Scala type conversions

Tags:

scala

clojure

I'm having trouble converting a Clojure Seq to a Scala Seq. it seems like there should be an easy way to do this.

(let [animals ["dog" "cat" "pig"]]
  (to-scala-seq animals))

My goal is to use twitter's algebird in a Clojure project.

like image 992
user906121 Avatar asked Oct 03 '22 16:10

user906121


1 Answers

Because Clojure and Scala both run on the JVM, this is fairly straight-forward:

 (ns scala-from-clojure.core
   (:import (scala.collection JavaConversions)))

 (defn to-scala-seq [coll]
   (-> coll JavaConversions/asScalaBuffer .toList))

At the REPL

user=> (use 'scala-from-clojure.core)
nil
user=> (to-scala-seq [1 2 3])
#<$colon$colon List(1, 2, 3)>
user=> (instance? scala.collection.immutable.Seq *1)
true

See

How to create a scala.collection.immutable.Seq from a Java List in Java?

like image 73
noahlz Avatar answered Oct 21 '22 05:10

noahlz