reify

Get a Haskell record's field names as a list of strings?

半城伤御伤魂 提交于 2020-01-01 04:11:08
问题 Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) --> ["alpha", "beta", "phi"] There is also a claim that this can be done without Template Haskell. Can someone provide an example implementation for this can be accomplished? 回答1: It can be done with a Data (most GHC versions) or Generic (7.2.x and up) instance, which

Use a clojure macro to automatically create getters and setters inside a reify call

淺唱寂寞╮ 提交于 2019-12-22 05:27:10
问题 I am trying to implement a huge Java interface with numerous (~50) getter and setter methods (some with irregular names). I thought it would be nice to use a macro to reduce the amount of code. So instead of (def data (atom {:x nil})) (reify HugeInterface (getX [this] (:x @data)) (setX [this v] (swap! data assoc :x v))) I want to be able to write (def data (atom {:x nil})) (reify HugeInterface (set-and-get getX setX :x)) Is this set-and-get macro (or something similar) possible? I haven't

How to get the declaration of a function using `reify`?

前提是你 提交于 2019-12-21 03:55:20
问题 Function reify allows me to look up information about a given name. For a function the returned value is VarI: data Info = ... | VarI Name Type (Maybe Dec) Fixity | ... Here I can examine the function's type, and I'd also like to examine its declaration. However, in the 3rd argument to VarI I always see Nothing . Is there a way to get the function's declaration? 回答1: From the template haskell docs on the VarI Info contructor: A "value" variable (as opposed to a type variable, see TyVarI ).

Use a clojure macro to automatically create getters and setters inside a reify call

左心房为你撑大大i 提交于 2019-12-05 07:03:36
I am trying to implement a huge Java interface with numerous (~50) getter and setter methods (some with irregular names). I thought it would be nice to use a macro to reduce the amount of code. So instead of (def data (atom {:x nil})) (reify HugeInterface (getX [this] (:x @data)) (setX [this v] (swap! data assoc :x v))) I want to be able to write (def data (atom {:x nil})) (reify HugeInterface (set-and-get getX setX :x)) Is this set-and-get macro (or something similar) possible? I haven't been able to make it work. (Updated with a second approach -- see below the second horizontal rule -- as

How to reify Prolog's backtracking state to perform the same task as “lazy seq” from Clojure?

帅比萌擦擦* 提交于 2019-12-05 04:38:44
Here is a quicksort algorithm for numbers written in Clojure. It is basically the quicksort algorithm found in "The Joy of Clojure" , 2nd edition, page 133. I modified it slightly for (hopefully) better readability, because the original felt a bit too compact: (defn qsort-inner [work] (lazy-seq (loop [loopwork work] (let [[ part & partz ] loopwork ] (if-let [[pivot & valuez] (seq part)] (let [ smaller? #(< % pivot) smz (filter smaller? valuez) lgz (remove smaller? valuez) nxxt (list* smz pivot lgz partz) ] (recur nxxt)) (if-let [[oldpivot & rightpartz] partz] (cons oldpivot (qsort-inner

Get a Haskell record's field names as a list of strings?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 10:31:10
Say I have the following: data Rec = Rec { alpha :: Int, beta :: Double, phi :: Float } sample = Rec 1 2.3 4.5 I understand Template Haskell & the reify function can get me the record's field names. That is: print $(f sample) --> ["alpha", "beta", "phi"] There is also a claim that this can be done without Template Haskell. Can someone provide an example implementation for this can be accomplished? It can be done with a Data (most GHC versions) or Generic (7.2.x and up) instance, which GHC can derive for you. Here's an example of how to dump record fields with the Data typeclass: {-# LANGUAGE

How to reify Prolog's backtracking state to perform the same task as “lazy seq” from Clojure?

为君一笑 提交于 2019-12-01 02:07:49
问题 Here is a quicksort algorithm for numbers written in Clojure. It is basically the quicksort algorithm found in "The Joy of Clojure" , 2nd edition, page 133. I modified it slightly for (hopefully) better readability, because the original felt a bit too compact: (defn qsort-inner [work] (lazy-seq (loop [loopwork work] (let [[ part & partz ] loopwork ] (if-let [[pivot & valuez] (seq part)] (let [ smaller? #(< % pivot) smz (filter smaller? valuez) lgz (remove smaller? valuez) nxxt (list* smz