Andrey Hihlovskiy
Professional blog on groovy, gradle, Java, Javascript and other stuff.
Tag Archives: programming
How to teach your kids to draw fractals
February 10, 2014
Posted by on It’s probably one of the best things that computer-geek dad/mom can teach their kid – to draw fractals with LOGO. Here is how you do it:
- Install Kturtle or similar LOGO software
- Refresh your knowledge on fractals (for example, at the excellent website of Jeffrey Ventrella)
- Look at the examples of turtle graphics and fractals on the Internet
- Repeat (together with your kid) very basic stuff from geometry: how to add/subtract angles, triangle postulate, etc.
Then fun begins:
- 30 minutes exercise implementing Koch Curve
- 1 hour exercise implementing Sierpinsky Triangle
- 1.5 hour exercise implementing various fractal trees
- 1 hour implementing Dragon Curve
- etc. etc.
Some very important things to remember:
- don’t overload your kid with knowledge. It’s important to get to exercises ASAP, otherwise motivation is lost.
- at first do every exercise yourself, without the kid. This way you make sure that you understand the goals and methods of implementation and can deliver that understanding to a kid.
- be clear at formulating goals and algorithms, show on paper how stuff works.
- don’t try to be overly supportive, give a kid space to think and experiment with implementation.
- ask kid questions and stimulate thinking: “what about changing this parameter here, how will it affect our fractal”?
- don’t try to do all exercises at once – this would not be effective, since the brain needs time to digest new concepts.
This could be the beginning of your kids’ big love – love to mathematics and science.
Illustration: Sierpinsky Triangle, created by my daughter with Kturtle:
The source code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
learn triangle $x { | |
fw $x | |
tr 120 | |
fw $x | |
tr 120 | |
fw $x | |
tr 120 | |
} | |
learn serpinsky $x, $level { | |
triangle $x | |
pu | |
fw $x/2 | |
tr 60 | |
pd | |
serpinsky1 $x/2, $level | |
} | |
learn serpinsky1 $x, $level { | |
if $level == 0 { | |
return | |
} | |
triangle $x | |
pu | |
tl 60 | |
fw $x/2 | |
tr 60 | |
pd | |
serpinsky1 $x/2, $level – 1 | |
pu | |
tr 60 | |
fw $x | |
tl 60 | |
pd | |
serpinsky1 $x/2, $level – 1 | |
pu | |
tr 180 | |
fw $x | |
tl 180 | |
pd | |
serpinsky1 $x/2, $level – 1 | |
pu | |
tl 60 | |
fw $x/2 | |
tr 60 | |
pd | |
} | |
$level = 7 | |
reset | |
go 120, 0 | |
print "Serpinsky carpet level " + $level | |
pu | |
go 0, 400 | |
pd | |
dir 30 | |
serpinsky 400, $level |
groovy script for running jetty server
October 28, 2013
Posted by on The following script starts jetty server and opens the folder, specified on command line, for http access (read-only):
#!/usr/bin/env groovy @Grab('javax.servlet:javax.servlet-api:3.0.1') @Grab(group='org.eclipse.jetty', module='jetty-webapp', version='8.1.8.v20121106') @Grab(group='org.eclipse.jetty', module='jetty-server', version='8.1.8.v20121106', transitive=false) @Grab(group='org.eclipse.jetty', module='jetty-servlet', version='8.1.8.v20121106', transitive=false) @GrabExclude('org.eclipse.jetty.orbit:javax.servlet') import org.eclipse.jetty.server.Server import org.eclipse.jetty.servlet.* import groovy.servlet.* def publishedFolder = args ? args[0] : '.' def server = new Server(8080) def context = new ServletContextHandler(server, '/', ServletContextHandler.SESSIONS) def webappContext = new org.eclipse.jetty.webapp.WebAppContext(publishedFolder, '/jetty') context.setHandler(webappContext) server.start() println 'Jetty server started. Press Ctrl+C to stop.'
Usage:
- Save this script to file “jetty.groovy”
- Invoke on command-line:
groovy jetty.groovy /path/to/some/folder"
- Enter address in web-browser:
http://localhost:8080/jetty
Expected result: you see the content of the folder “/path/to/some/folder” in the web-browser.
Groovy language, spaceship operator
August 28, 2013
Posted by on x <=> y
Useful in comparisons:
- returns -1 if x is smaller than y
- return 0 if x equals to y
- returns 1 if x is greater than y.
Isn’t it sweet?
Partial interface implementation in groovy
August 6, 2013
Posted by on interface X { void a() void b() } class XAdapter implements X { void a() { println 'default implementation of a' } void b() { println 'default implementation of b' } } def o = [ a: { println 'overridden implementation of a' } ] as XAdapter o.a() o.b()
will output:
overridden implementation of a
default implementation of b
Gradle script: multiproject git-gradle management
June 7, 2013
Posted by on Suppose you build your software project from many open-source components, most of which are already available via git. How to automate clone/pull/build/install cycle, especially across projects from different git-repositories? How to establish high-level inter-project dependencies?
For that I wrote gradle script, which implements multiproject git-gradle management. It works as follows: you write configuration file, name it “config.gradle”, put it to the same folder as “build.gradle” (of multiproject git-gradle) and then run “gradle build”.
Full documentation and sources are available at:
Cargo-Culting in Javascript
June 2, 2013
Posted by on Nice article. Somewhat opinionated, but fun to read.
Recent Comments