Demo entry 6345777

Scala

   

Submitted by anonymous on Feb 06, 2017 at 23:27
Language: Rust. Code size: 1.0 kB.

// define gravitational constant using a vector
let g = <0, -9.8>
// Type inference of the compiler automatically assigns type float to the variable
let dt = 0.01 

co euler(particle[] init) -> particle[] {
	// @ operator used to map from last value of all_particles array to lambda function
	val particle[] all_particles = all_particles..1 @ p -> [
		// return a particle with updated position and velocity according to Euler’s method
		// note ease of expression with vector math
                var particle {
			.v = p.v + g * dt;
			.x = p.x + p.v * dt;
		}
	] : init;
// yield our final array
}.yield(all_particles);

// program entry point
fn main(void)
{
	// initialize a set of particles with *all* values to 0
	val particle[10] p_init = {0};
	
// @ operator used to map each yielded value of euler coroutine 
// to a display function at each state
// displayf uses opengl to render given a frame given a structure with positions
	loop 1000 euler(p_init) @ state -> [
		displayf(state);
	]
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).