displaying images frame by frame in the same window in Racket - functional-programming

I have three small images of same size. I want to display them one after another.
(define new1-bitmap
(make-bitmap
(send bird-bitmap get-width)
(send bird-bitmap get-height)))
(define dc-crop
(new bitmap-dc% [bitmap new1-bitmap]))
(define f-crop
(new frame% [label "Random"]))
(send f-crop show #t)
(send dc-crop draw-bitmap-section
bird-bitmap
0
0
0
(round(* (/ (send bird-bitmap get-height) 3) 2))
(send bird-bitmap get-width)
(round(/ (send bird-bitmap get-height) 3)))
(void
(new message% [parent f-crop] [label new1-bitmap]))
(sleep 3)
(send dc-crop draw-bitmap-section
new1-bitmap
0
0
0
(round(/ (send bird-bitmap get-height) 3))
(send bird-bitmap get-width)
(round(/ (send bird-bitmap get-height) 3)))
(void
(new message% [parent f-crop] [label new1-bitmap]))
(sleep 3)
(send dc-crop draw-bitmap-section
new1-bitmap
0
0
0
0
(send bird-bitmap get-width)
(round(/ (send bird-bitmap get-height) 3)))
(void
(new message% [parent f-crop] [label new1-bitmap]))
Above is the code that I thought would work. It only take three images and tries to show them one after another at an interval of 3 seconds. Moreover the final GUI is three times longer than others.
How should I do this?

A simple window with animation can be made using the 2htdp libraries. 2htdp/universe is a handy place to start prototyping simple applications that mainly produce side effects.
The example code uses some of the icons that ship with Racket for convenience.
#lang racket
(require 2htdp/image
2htdp/universe)
(define image1 (bitmap icons/stop-16x16.png))
(define image2 (bitmap icons/bug09.png))
(define image3 (bitmap icons/break.png))
(define image-list (list image1 image2 image3))
(run-movie 3 image-list)
A more sophisticated example using racket/gui requires implementing a timer% rather than using sleep because sleep acts on the thread level. It appears that when running directly from the source, Racket wants to queue both writes to the canvas then go to sleep and then empty the queue and make both writes in succession.
The second tricky bit is the nested send to access the canvas's drawing context.
The code example below is derived from this thread on the Racket email list. It displays an image of one of my current dogs, waits one second, then displays an image of one of my former dogs.
#lang racket/gui
(require 2htdp/image)
(provide (all-defined-out))
(define image1 (make-object bitmap% "scarlett.jpg"))
(define image2 (make-object bitmap% "witty2.jpg"))
(define my-frame (instantiate frame%("my frame")))
(define mcan%
(class canvas%
(override on-paint)
(define on-paint
(lambda()(send (send this get-dc)
draw-bitmap image1 0 0)))
(super-instantiate())))
(define mcan (new mcan% (parent my-frame)
(min-width (image-width image1))
(min-height (image-height image1))))
(define timer
(new timer%
(notify-callback
(lambda()
(send (send mcan get-dc)
draw-bitmap image2 0 0)))))
(send my-frame show #t)
(send timer start 1000)
A third alternative is to use sleep\yield as described in this post on the Racket discussion list.

Related

Representing a queue as a procedure with local state

Section §2.1.3 at page 90 explains, with a very clear example, that first class functions in a language make functions themselves and data be the same thing looked at from different perspectives, or, to cite the book:
the ability to manipulate procedures as objects automatically provides the ability to represent compound data.
At page 266, exercise 3.22 from Section §3.3.2, proposes the following
Instead of representing a queue as a pair of pointers, we can build a queue as a procedure with local state. The local state will consist of pointers to the beginning and the end of an ordinary list. Thus, the make-queue procedure will have the form
(define (make-queue)
(let ((front-ptr ...)
(rear-ptr ...))
<definitions of internal procedures>
(define (dispatch m) ...)
dispatch))
Complete the definition of make-queue and provide implementations of the queue operations using this representation.
I easily came up with the following (I've used names the-list and last-pair instead of front-ptr and rear-ptr because I found it clearer, in this case):
(define (make-queue)
(let ((the-list '())
(last-pair '()))
(define (dispatch m)
(cond ((eq? m 'empty) (null? the-list))
((eq? m 'front) (if (null? the-list)
(error "can't take front of empty list")
(car the-list)))
((eq? m 'ins) (lambda (e)
(if (null? the-list)
(begin (set! the-list (list e))
(set! last-pair the-list))
(begin (set-cdr! last-pair (list e))
(set! last-pair (cdr last-pair))))
the-list))
((eq? m 'del) (begin
(if (null? the-list)
(error "can't delete from emtpy list")
(set! the-list (if (pair? the-list) (cdr the-list) '())))
the-list))
((eq? m 'disp) (display the-list)) ; added this for convenience
(else "error")))
dispatch))
(define (empty-queue? q) (q 'empty))
(define (front-queue q) (q 'front))
(define (insert-queue! q e) ((q 'ins) e))
(define (delete-queue! q) (q 'del))
(define (display-queue q) (q 'disp))
which seems to work pretty fairly well…
… except for one crucial point!
At the beginning of §3.3.2 the two desired mutators (that are part of the queue interface) are defined like this (my emphasis):
(insert-queue! <queue> <item>)
inserts the item at the rear of the queue and returns the modified queue as its value.
(delete-queue! <queue>)
removes the item at the front of the queue and returns the modified queue as its value, signaling an error if the queue is empty before the deletion.
My solution doesn't abide by those parts of the definition, because both insert-queue! and delete-queue! are returning the-list, which is the bare list, an implementation detail of the queue interface. Indeed, my solution doesn't support things like these
(define q (make-queue)) ; ok
(insert-queue! (insert-queue! q 3) 4) ; doesn't work
(delete-queue! (delete-queue! q)) ; doesn't work
whereas I think it should.
I guess that the solution should see delete-queue! and insert-queue! return a mutated version of the dispatch function.
How do I do that?
No need for that. Simply define
(define (insert-queue! q e)
((q 'ins) e)
q)
(define (delete-queue! q)
(q 'del)
q)
The design is not clean though, as in, these queues are not persistent. The new version and the old share the same underlying buffer (list). There is no old version preserved anymore, just the current version.
So we don't return a new, modified queue; we return the same queue which has been mutated. Conceptually, that is. On a little bit lower level, we return the same dispatch procedure which is a part of the same closure which holds the same internal binding for the internal buffer, which has been mutated.
By the way, using the head sentinel trick, were you start with e.g. (list 1) instead of '(), usually leads to much simplified, clearer code.

RACKET pause/continue button for game

I have a school project to create a version of Tetris in racket and I have to
implement a pause/continue button which I'm stuck on.. My try:
(define *my-timer*
(new timer%
[notify-callback (lambda () (send *my-game-canvas* refresh))]))
(define *pause*
(new button%
[parent *my-window*]
[label "Pause"]
[callback (lambda ()
(send *my-timer* stop))]))
At the moment only the pause function is implemented but it doesn't work. I get an error message on the second define.
*my-game-canvas * is the canvas there the game graphics take place.
I appreciate all the answers.
/ Kasper
Edit:
The error I get:
initialization for button%: contract violation
expected: (procedure-arity-includes/c 2)
given: #<procedure:...0160511/game.rkt:61:17>
The problem is this clause:
[callback (lambda ()
(send *my-timer* stop))]
A callback needs to take two arguments: the button and the event.
Try this:
[callback (lambda (button event)
(send *my-timer* stop))]

RACKET Callback procedure to load canvas% via button%

I'm doing a game project in Racket and I'm stuck on how I can create a callback procedure for my button so I can go from my start menu to my game.. I have this button defined:
(define *starta-spelet*
(new button%
[parent *menyruta*]
[label "Starta Spelet"]
[min-width 130]))
And there is a callback argument for button% (look here https://docs.racket-lang.org/gui/button_.html) but I dont know how I should define my procedure which takes me from my start menu (which contains panel% and fram%) to my game (which is built on a canvas).
Also, the game and the start-menu is created in separate files .rkt files
I appreciate all help I can get. Let me know if I something is unclear.
Here is an example matching your description.
Note that the changes to the content of the-frame is made
in between start-container-sequence and end-container-sequence.
This is done to prevent the system in rendering the GUI before
we have added the game-canvas.
#lang racket
(require racket/gui)
;;; GUI
;; The frame holds either a start-panel or a game-panel
(define the-frame (new frame% [label "A frame"] [min-width 200] [min-height 200]))
;; The start-panel contains a start button
(define (make-start-panel)
(define start-panel (new panel% [parent the-frame]))
(define start-button (new button% [parent start-panel] [label "Start"]
[callback (λ (b e) (on-start-button b e))]))
start-panel)
;; The game-panel contains a canvas
(define (make-game-panel)
(define game-panel (new panel% [parent the-frame])) ; will be set to the-frame later
(define game-canvas (new canvas% [parent game-panel] [min-width 200] [min-height 200]))
game-panel)
;;; Event Handlers
(define (on-start-button button event)
(send the-frame begin-container-sequence)
(send the-frame delete-child the-start-panel)
(make-game-panel)
(send the-frame end-container-sequence))
;;; Begin Program
(define the-start-panel (make-start-panel))
(send the-frame show #t)

Avoiding mutable state when I need to store (current-seconds) multiple times

I've put together the following rudimentary stopwatch in Racket (just learning now, the final aim is a pomodoro-timer).
#lang racket
(define start-time 0)
(define end-times '())
(define (start);; stores start-time
(set! start-time (current-seconds)))
(define (lap);; stores "laps" in list
(set! end-times (cons (current-seconds) end-times)))
(define (stop);; stores final time, displays lap-times in h, m, s and resets end-times
(begin
(set! end-times (cons (current-seconds) end-times))
(display
(reverse
(map (lambda (an-end)
(let ((the-date (seconds->date(- an-end start-time))))
(list
(sub1(date-hour the-date))
;; sub1 is needed because (date-hour(seconds->date 0) = 1
(date-minute the-date)
(date-second the-date)))) end-times)))
(set! end-times '())
))
While this does exactly what it should, I was wondering how I could avoid mutable state. If I follow HTDP, this is the kind of situation where mutable state is warranted, but after browsing Wadler's "Monads for Functional Programming", I'm still curious about how I could do without set!.
I know that to make it functional, I should add arguments to my functions. For instance, start would become
(define (start [now (current-seconds)])
now)
and a similar approach could work with lap and stop.
Still, while I know that after adding additional arguments to restore functionality, I should also pass arguments rather than storing values in variables, I don't see how in this case I can leverage this to avoid set! as well.
Update: Since all three answers below are highly valuable (thanks!), I didn't mark any of them as the unique correct one. Below is the minimal solution to my initial question. It is a combination of the loop-proposal of #Metaxal, with the example-usage of #Greg Hendershott.
#lang racket
(define (run)
(displayln "Enter 'lap' or 'quit':")
(let loop ([t0 (current-seconds)] [times '()])
(match (read-line)
["quit" (reverse
(map (lambda (x)
(let ((the-date (seconds->date x)))
(list
(sub1(date-hour the-date))
(date-minute the-date)
(date-second the-date)))) times))]
["lap" (loop t0 (cons (- (current-seconds) t0) times))]
[_ (loop t0 times)])))
What will likely happen in the following of your program is that you will have a loop.
Then this loop can be a function that takes as input the whole current state, and when you want to update its state, just call the loop again with the new state (you may also call the loop again with the same exact state of course).
Simplified example:
(define (loop [t0 (current-seconds)] [times '()])
;; ... do things here, possibly depending on user input ...
;; then loop with a new state:
(cond [<some-start-condition> (loop (current-seconds) '())]
[<some-lap-condition> (loop t0 (cons (- (current-seconds) t0) times))]
[<some-stop-condition> times])) ; stop, no loop, return value
This certainly changes the approach to your design though.
It's harder to use this approach when designing GUI programs, because the event loop often prevents you from (or makes it difficult) passing values from one event to the next.
However, in Racket, there is (the pedagogical, but still very good) big-bang that is made just for that.
In this case using set! is justified and hard to avoid, because we must "remember" state between invocations of the procedures. What we can do is improving the encapsulation of state, by hiding the variables that change inside a procedure and using a message dispatcher for accessing the procedures that refer to the mutable state. This is very similar to what we do with object-oriented programming, but only lambdas are required to implement it!
(define (make-timer)
; the "attributes" of the object
(let ([start-time 0]
[end-times '()])
; the "methods" of the object
(define (start)
(set! start-time (current-seconds)))
(define (lap)
(set! end-times (append end-times (list (current-seconds)))))
(define (stop)
(lap)
(display
(map (lambda (an-end)
(let ((the-date (seconds->date (- an-end start-time))))
(list
(sub1 (date-hour the-date))
(date-minute the-date)
(date-second the-date))))
end-times))
(set! end-times '()))
; return a dispatch procedure
(lambda (msg)
(case msg
((start) (start)) ; call the start procedure defined above
((lap) (lap)) ; call the lap procedure defined above
((stop) (stop)) ; call the stop procedure defined above
(else (error "unknown message:" msg))))))
I took the liberty of modifying some of your procedures to make them a bit simpler. Here's how we would use the timer object we just created:
(define timer (make-timer))
(timer 'start)
(sleep 1)
(timer 'lap)
(sleep 1)
(timer 'lap)
(sleep 1)
(timer 'lap)
(sleep 1)
(timer 'stop)
=> ((18 0 1) (18 0 2) (18 0 3) (18 0 4))
This technique is called "message passing", learn more about it in the wonderful SICP book.
For a simple example like this, I would probably do what #Metaxal
suggested.
However another approach is that you could explicitly define the state
as a struct:
(struct state (start-time end-times))
Then change the start, lap, and stop to be functions on state:
;; start : -> state
;; stores start-time
(define (start)
(state (current-seconds) '()))
;; lap : state -> state
;; stores "laps" in list
(define (lap st)
(match-define (state start-time end-times) st)
(state start-time
(cons (current-seconds) end-times)))
;; stop : state -> list
;; stores final time, displays lap-times in h, m, s
(define (stop st)
(match-define (state start-time end-times*) st)
(define end-times (cons (current-seconds) end-times*))
(reverse
(map (lambda (an-end)
(let ((the-date (seconds->date(- an-end start-time))))
(list
(sub1(date-hour the-date))
;; sub1 is needed because (date-hour(seconds->date 0) = 1
(date-minute the-date)
(date-second the-date)))) end-times)))
As in #Metaxal's answer, your "main loop" needs to handle the state and "thread" it through the functions as appropriate:
Example usage:
(define (run)
(displayln "Enter 'lap' or 'quit':")
(let loop ([st (start)])
(match (read-line)
["quit" (stop st)]
["lap" (loop (lap st))]
[_ (loop st)])))
Whereas #Óscar López's answer shows a style of OOP as explained in SICP.
A nice thing about Racket (and Scheme) is that you can choose whatever approach on the spectrum you think best fits the problem at hand, and your taste -- simple imperative, OOP imperative, pure functional.

Emacs automating function based on timer: user disturbance

I am looking for a general way for Emacs to do some checks, and (bing) when something is true or false.
I have 2 examples at the moment. There is an auction website, and I figured it would be nice for Emacs to check whether there has been an update. I did this using R (Emacs-ESS) to load source code of the website. Then I use a selfmade function in Emacs to:
Switch buffer, reload the URL info, send the info to the screen, (sleep-for ) to wait a little. Search for "Today". Then it has to check the value after this string, if it is higher than 0, then it means I have something new of interest, and it PINGS.
This is a long introduction, but I really like that this works.
I now do the same with Gnus, I have a timer that runs a function that opens Gnus every 10 seconds, searches for "Inbox", checks the value, and if it is higher than 0 then it notifies me (otherwise it just switches the buffer back). The only problem is that this takes around 0.5 seconds, in which you can see point move to another buffer and switch back.
Is there a general approach to have these automated things be done, without disturbing the user?
EDIT: Wouldn't it be nice for Emacs to perform a check to see whether there is a new post with our favorite tag here on Stackoverflow?
Emacs is single-threaded, so we are screwed - there is not way to do this without bothering the user at all.
The trick is to select a good balance between the frequency and cost of the check so that the user can bear it.
Here is the code I used once:
(defvar sds-new-mail-line nil "cache")
(defun sds-new-mail-line (&optional arg)
"add or remove the mode-line new-mail marker"
(or sds-new-mail-line (error "sds-new-mail-line has not been initialized"))
(let* ((mlf (default-value 'mode-line-format))
(already-have (eq sds-new-mail-line (car mlf))))
(if (or (eq arg nil) (< arg 0))
(when already-have
(setq-default mode-line-format (cdr mlf)))
(unless already-have
(setq-default mode-line-format (cons sds-new-mail-line mlf))))))
(defun sds-gnus-scan-mail ()
"check for new mail, notify if there is some"
(when (gnus-alive-p)
(with-current-buffer gnus-group-buffer
(gnus-group-get-new-news 3)
(gnus-group-get-new-news 2)
(goto-char (point-min))
;; look for new messages in groups of level 1 and 2
(cond ((search-forward-regexp "^ *s[12] *[1-9][0-9]*n" nil t)
(message "you have new mail! (%s)" (user-time-format))
(sds-new-mail-line 1)
(ding))
(t (sds-new-mail-line -1)
(message "no new mail (%s)" (user-time-format))))
(goto-char (point-min)))))
(defun sds-gnus-load-hook ()
(unless sds-new-mail-line ; init
(let ((str "mail") (map (make-sparse-keymap)))
(define-key map [mode-line down-mouse-1] 'ignore)
(define-key map [mode-line mouse-1] read-mail-command)
(add-text-properties 0 (length str)
(list 'display gnus-mode-line-image-cache
'help-echo "you have new mail - read it!"
'local-map map)
str)
(setq sds-new-mail-line str))
(gnus-demon-add-handler 'sds-gnus-scan-mail 3 t))
(add-hook 'gnus-summary-prepared-hook 'gnus-summary-first-unread-subject)
(add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-catchup))
;; cannot use gnus-load-hook here!
(eval-after-load "gnus-start" '(sds-gnus-load-hook))
I am sure you can adapt it to your needs.

Resources