How to pass a list of integers from Clojure to a Frege function?

Inspired by the previous question What is the easiest way to pass a list of integers from java to the frege function? and comment in answers from @Ingo, I tried

(Foo/myfregefunction (java.util.List. [1,2,3,4]))

but get (ctor = constructor):

CompilerException java.lang.IllegalArgumentException: No matching ctor found for interface java.util.List

Any ideas? At least java.util.Listnot throwing ClassCastException; Does this mean that it is on the right track?

I can send Frege almost any type of Java collection from Clojure, see Converting Clojure Data Structures to Java Collections .

BTW, using plain (Foo/myfregefunction [1,2,3,4]), instead gives ClassCastException clojure.lang.PersistentVector cannot be cast to free.runtime.Lazywhat @Ingo points out, "A Clojure list is not a frege list." A similar answer when casting java.util.ArrayList.

On the Frege side, the code is similar to

module Foo where

myfregefunction :: [Int] -> Int
-- do something with the list here
+4
2

, Clojure, , , , (.. java.util.ArraList), java.util.List .

, , .

, , java . ,

βˆ€ s a. Mutable s (List a) β†’ [a]

​​ .

ST ( myfregefunction). ST - , . :

import Java.Util(List, Iterator)   -- java types we need

fromClojure !list = 
    List.iterator list >>= _.toList >>= pure . myfregefunction

Clojure - ( , clojure ( )):

(frege.prelude.PreludeBase$TST/run (Foo/fromClojure (java.util.ArrayList. [1,2,3,4])))

Java : IMHO. -, , Frege , . , . , clojure, , , , Frege.

, - , clojure.lang.PersistentVector, clojure Frege. -, clojure -, , , .

( , Clojure/Frege!)

: @0dB, , . .

- Frege Clojure.

+5

@Ingo,

, clojure.lang.PersistentVector clojure Frege.

, PersistentMap , :

module foo.Foo where

[] , ListView , head, tail,...

instance ListView PersistentVector

clojure Frege (pure native Java Frege, - , , , Clojure, ):

data PersistentVector a = native clojure.lang.IPersistentVector where
  -- methods needed to create new instances
  pure native empty clojure.lang.PersistentVector.EMPTY :: PersistentVector a
  pure native cons :: PersistentVector a -> a -> PersistentVector a
  -- methods needed to transform instance into Frege list
  pure native valAt :: PersistentVector a -> Int -> a
  pure native length :: PersistentVector a -> Int

, clojure Frege :

  fromList :: [a] -> PersistentVector a
  fromList = fold cons empty

  toList :: PersistentVector a -> [a]
  toList pv = map pv.valAt [0..(pv.length - 1)]

"" ; . @Dierk, .

[EDIT] ListView ( Frege PersistentVector) uncons, null take ( , ):

  null :: PersistentVector a -> Bool
  null x = x.length == 0

  uncons :: PersistentVector a -> Maybe (a, PersistentVector a)
  uncons x
    | null x = Nothing
    -- quick & dirty (using fromList, toList); try to use first and rest from Clojure here
    | otherwise = Just (x.valAt 0, fromList $ drop 1 $ toList x)

  take :: Int -> PersistentVector a -> PersistentVector a
  -- quick and dirty (using fromList, toList); improve this
  take n = fromList β€’ PreludeList.take n β€’ toList

PreludeList.take, take , PersistentVector, fromList, toList, cons empty.

uncons, null take, instance , PersistentVector Frege ). , , :

fromClojure :: PersistentVector a -> PersistentVector a
fromClojure = PersistentVector.fromList β€’ myfregefn β€’ PersistentVector.toList

-- sample (your function here)
myfregefn :: [a] -> [a]
myfregefn = tail

clojure (foo.Foo/fromClojure [1 2 3 4]) clojure myfregefn ( [2 3 4]). myfregefn , clojure, Frege (String, Long,...), PersistentVector.fromList ( ). , tail, , head , , Long String.

, ', e. . PersistentVector a [a].

: , clojure Frege, " ". , , , Ingo, .

+1

Source: https://habr.com/ru/post/1620963/


All Articles