Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Clojure and Swing

Name: Anonymous 2010-03-05 1:45

Hey /prog/riders, I'm not a LISPer but I'm learning. I was wondering if it were possible to dynamically create a grid of buttons in swing, instead of initializing all of them individually. I was thinking of making a tic-tac-toe game, but I can't work out how to make it work without making nine specific variable buttons.

Name: Anonymous 2010-03-05 2:07

Wait, LISP makes GUI by stealing from other languages?

Name: Anonymous 2010-03-05 2:10

Well Clojure seems to... But Clojure isn't as LISPie as other languages.

Name: Anonymous 2010-03-05 2:30

>>2
The alternative is to use CLIM, but then only Lispers would touch your program, so you might as well just skip the GUI and give it a programmatic interface.

Name: Anonymous 2010-03-05 3:49

>>2
Wait, PYTHON makes GUI by stealing from other languages?
Wait, HASKAL makes GUI by stealing from other languages?
Wait, RUBY makes GUI by stealing from other languages?
...and so on

Name: Anonymous 2010-03-05 6:22

Wait, EVERYTHING makes GUI in C?

Name: Anonymous 2010-03-05 6:36

>>1
I don't know if I understand the question correctly but you want to create a clickable tic-tac-toe board in Swing?
Obviously the best practices / most scalable way is to use a GridBagLayout backed by a 2D-Array, each grid/aray square containing an instance of some "Square" class which extends JPanel. In this way you wouldn't really need to specifically instantiate buttons yourself but they would be created inside the Square class as you add squares to your GBL.

If you don't want to do it this way you could just use a custom Graphics and calculate clicked squares based on x,y co-ordinates and use manual line, circle drawing.

The best option however, would be to just write the application using HTML/AJAX and have a horizontally scalable turn-key tic-tac-toe implementation in the cloud.

Name: Anonymous 2010-03-05 8:11

>>1
There was a thread which listed like at least a dozen CL-based GUIs for a variety of platforms(some cross-platform). Go search /prog/scrape for it. Too bad I already looked it up for you:
http://dis.4chan.org/read/prog/1264252528

>>2
Lisp's pretty good at stealing good ideas from other languages and incorporating them in its ball of Mud. It's one of those things that make Lisp Lisp.

Name: Anonymous 2010-03-05 8:21

>>8
I actually want to make the application in swing...

>>7
Will try to do it... Never done anything but imperative and OO programming so far, kind of hard figuring out functional.

Name: Anonymous 2010-03-05 8:26

>>9
Clojure(and most other Lisps) is not purely functional. If you need to modify state, you can do it.
It should be fine as long as you don't define a variable without binding anything to it, then force assign a value to it (there is actually a reason to do this in some Lisps, such as those that don't have a direct letrec form for variables - but it's possible to define such a form using a macro).

Name: Anonymous 2010-03-05 11:41

Override the paintComponent method on a JPanel.

Name: Anonymous 2010-03-05 13:41

OVERRIDE MY ANUS

Name: Anonymous 2010-03-05 16:38

>>8
Other languages are pretty good at stealing ideas from Lisp's ball of mud and passing them off as their own.
FTFY

Name: Anonymous 2010-03-05 18:53

>>13
While it's nice to be a smug lisp weenie, we should give credit where it is due. OO wasn't invented in Lisp, even if CL was the first to have it in a standard. Aspect oriented programming wasn't invented by lispers, but lisp supports it. Lisp didn't invent lexical scoping, Algol did...and so on.

Name: Anonymous 2010-03-05 19:21

>>14
Yes, Lisp is excellent in taking concepts from other languages and incorporating it in itself (usually so easy that mere users of the language can do it portably in most cases, and de-facto portable(implementation specific additions across popular lisps) in the few exceptions).

OO wasn't invented in Lisp, even if CL was the first to have it in a standard.

CLOS(+MOP) is still one of the fanciest OO systems I've ever seen(and I'd argue much more flexible than some others), it also came much sooner than so-called 'modern' OO systems.

Lisp didn't invent lexical scoping
Scheme did, and you could say that it's a lisp to some degree.
Lisp also kept dynamic scoping optional which is basically global variables done right.

Name: Anonymous 2010-03-05 19:35

>>15
Scheme did
Then perhaps I've mistaken block structuring and lexical scoping W-|
CLOS(+MOP) is still one of the fanciest OO systems I've ever seen(and I'd argue much more flexible than some others), it also came much sooner than so-called 'modern' OO systems.
I agree, but this one is really just personal preference. Some people like prototypes and they have io; some like generic functions, they have CLOS; other like conflating ADTs and objects, they have Java.

Name: Anonymous 2010-03-05 19:42

>>15
Scheme did, and you could say that it's a lisp to some degree.
How are we defining "a Lisp"? If Scheme is only a Lisp to a certain degree, the what about Arc? Newlisp? Clojure? even say Dylan?

Name: Anonymous 2010-03-05 20:08

>>17
It's in the Lisp family of languages, it shares many features with what was LISP around its time, but while functions and scope were changed(among other things), it retained Lisp's syntax as well a lot of its semantics. Scheme never officially supported low-level macros or reader macros as well as some other features present in more "real lisps", though you could get these features non-portably across implementations. Arc is rather simple, but it retains many base "Lisp" features, so its also a Lisp. newLISP is a new LISP, but its design is very antiquated(always dynamic scope and FEXPRs, as fully interpreted - have we not learned anything in the past 50 years?) - to its core it's still derived from other Lisps, so it is a Lisp, even if they could have chose better features to put in it. Dylan - this is a tough one, it heavily borrows a lot of Lisp semantics, but one very important core requirement to Lisps is "code=data" which it cannot retain if you're not using a homoiconic syntax, so it's hard to call it a Lisp, but given the close semantics(more close than some other "LISP"'s) you could call it a Honorary Lisp, even though there is no proper "code=data".

Name: Anonymous 2010-03-05 23:42

I finished it. Took me ages to think up a LISPy way to sort it, I stole the idea from a wiki but I implemented it myself. First Java/Lisp thing I've ever made. I know it's shit, I just don't like leaving things unfinished.

(import '(javax.swing JFrame JButton)
 '(java.awt.event ActionListener)
 '(java.awt GridLayout))

(def player (ref true))
(def length 3)
(def size (* length length))
(def boxes (make-array JButton size))

(defn clear-boxes []
  (loop [idx 0]
    (when (< idx size)
      (.setText (aget boxes idx) "")
      (recur (inc idx)))))

(defn get-boxes [string list idx]
  (if (= idx size) list
    (if (= (.getText (aget boxes idx)) string)
      (get-boxes string (cons idx list) (inc idx))
      (get-boxes string list (inc idx)))))

(defn sum-boxes [list idx idt]
  (let [n-val '(2 9 4 7 5 3 6 1 8)]
    (cond
      (= idx 0) (= idt 0)
      (< idt 0) nil
      (empty? list) nil
      (sum-boxes (rest list) (- idx 1) (- idt (nth n-val (first list)))) true
      (sum-boxes (rest list) idx idt) true)))

(defn check-boxes [string]
  (or sum-boxes (get-boxes string '() 0) length 15) (empty? (get-boxes "" '() 0)))

(defn set-box [temp]
  (when (= (.getText temp) "")
    (dosync
      (if (deref player)
        (do
          (.setText temp "x")
          (ref-set player false)
          (if (check-boxes "x") (clear-boxes)))
        (do
          (.setText temp "o")
          (ref-set player true)
          (if (check-boxes "o") (clear-boxes)))))))

(let [frame (new JFrame "NAC")]
  (loop [idx 0]
    (when (< idx size)
      (let [temp (new JButton "")]
        (doto temp
          (.addActionListener
            (proxy [ActionListener] []
              (actionPerformed [evt]
                (set-box temp)))))
        (doto frame
          (.add temp))
        (aset boxes idx temp))
      (recur (inc idx))))
  (doto frame
    (.setDefaultCloseOperation (. JFrame EXIT_ON_CLOSE))
    (.setLayout (new GridLayout 3 3 3 3))
    (.setSize 200 200)
    (.setVisible true)))

Name: Anonymous 2010-03-06 0:21

dickbutt

Name: Anonymous 2010-03-06 0:39

>>19
You know, I know nothing about LISP syntax, but that's the first LISP code posted here that I understood.

Name: Anonymous 2010-03-06 0:52

>>16
Some people like prototypes and they have io

Wow, I never hear anyone speak of Io. I like it for what I know of it. It does seem very apt to me, with a few rough edges in the implementation.

Name: Anonymous 2010-03-06 3:56

>>21
That's because most of us post intentionally obfuscatory code. Same goes for the Haskellers.

Name: Anonymous 2011-02-03 6:45

Don't change these.
Name: Email:
Entire Thread Thread List