Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-255: add py! macro with enhanced(?) data transformation capabilities #256

Closed
wants to merge 1 commit into from

Conversation

jjtolton
Copy link
Contributor

@jjtolton jjtolton commented Oct 19, 2023

"Closes": #255

(require '[libpython-clj2.sugar :refer [py!]])
(require '[libpython-clj2.python :refer [py..])
(require '[libpython-clj2.require :refer [require-python])
(require-python '[numpy :as np])
(require '[tech.v3.dataset :as ds])

(py! (python/dict {:a 1 :b 2})
     ->jvm
     (->> (repeat 3) (ds/->dataset))
     ->python
     ->jvm
     (->> vals (apply concat))
     (as-> $ (apply + $))
     (->> (repeat 10))
     (->> (partition 3))
     ->python
     (-> np/array)
     sum) ;;=> 81
(defn times-11 [n]
  (py! (python/list [1 2 3])
        __len__
        (as-> $ (+ 2 $))
        (some-> vector (get 3) (+ 3))
        (cond->
         nil?
          (conj 1))
        (-> first)
        (as-> $ (python/list [1 2 3 $ 4]))
        (->> (repeat n)
             (transduce
              (map (fn [args]
                     (apply + args)))
              (completing +)
              0))))

(- (times-11 4) 2) ;;=> 42
(py! (python/list)
     (doto
      (py.. (append 1))
      (py.. (append 1))
      (py.. (append 1)))
     (as-> $
           (cond-> $
             (< (count $) 4)
             (doto
              (py.. (append 5)))
             (< (count $) 6)
             (doto
              (py.. (append 7)))))) => [1, 1, 1, 5, 7]

😐

move to sugar, add doto

add string to sugar
@jjtolton
Copy link
Contributor Author

jjtolton commented Oct 19, 2023

This is a little out of control so I put it in sugar instead of py. Not sure we should be encouraging this madness. Also the cond is a little reflection heavy so there are some performance concerns there on tight loops. Great for exploration though. Also I suppose there is an infinitesimal chance that someone somewhere wrote a Python class with a .doto() method -- but I've never seen one. So I felt pretty safe adding that in.

@jjtolton jjtolton changed the title ISSUE-255: add py! macro with enhanced data transformation capabilities ISSUE-255: add py! macro with enhanced(?) data transformation capabilities Oct 19, 2023
@jjtolton
Copy link
Contributor Author

jjtolton commented Nov 7, 2023

Closing for now, I'm thinking this might be too out there for libpython-clj core but might be better as an ecosystem addition.

@jjtolton jjtolton closed this Nov 7, 2023
@jjtolton jjtolton deleted the ISSUE-255/data-transformation-pipelines branch November 7, 2023 05:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant