Skip to content

Instantly share code, notes, and snippets.

View nicolaspayette's full-sized avatar

Nicolas Payette nicolaspayette

View GitHub Profile
@nicolaspayette
nicolaspayette / show_packages_used.R
Created November 25, 2022 16:38
Print a sort list of packages used in an R script file.
# Prints a sort list of packages actually used in a script file.
# Note that the packages must be loaded for this to work.
show_packages_used <- \(script_file_name) {
script_file_name %>%
NCmisc::list.functions.in.file() %>%
names() %>%
stringr::str_extract_all("(?<=package:).\\w*", simplify = TRUE) %>%
c() %>%
purrr::keep(~ . != "") %>%
unique() %>%
@nicolaspayette
nicolaspayette / against-globals.md
Created August 4, 2016 13:44
Why you should probably avoid global variables in NetLogo

In my experience, a disproportionate amount of bugs in NetLogo models are caused by the mismanagement of global variables.

Here are a few reasons why I think that is the case, in (roughly) decreasing order of importance:

  • Code is not just a way of getting the computer to do what you want it to do—it is a way of conveying meaning to the reader of your code. When you declare a global variable, the meaning you convey is: “Hey! This thing is important throughout the whole program. You should always be paying attention to it.”

    Sometimes it’s justified , but very often, it is not. An unjustified global is not only failing to convey the right message: it’s actively misleading.

When I approach a model’s code for the first time, the first thing I do is look at the declarations on top: what breeds do we have, what agent variables do we have, and what global variables do we have? That gives me the “big picture” and tells me what I should be paying attention to when trying to understand what the code is do

to-report max-one-of-list [ the-list reporter-task ]
let results map reporter-task the-list
let highest max results
let indices n-values length the-list [ ? ]
let indices-with-highest filter [ item ? results = highest ] indices
report item (one-of indices-with-highest) the-list
end
[info] Running org.nlogo.models.CodeComplexity
[info] Exponential Growth, 0.002638522427440633
[info] Wolf Sheep Predation (System Dynamics), 0.002638522427440633
[info] Logistic Growth, 0.002638522427440633
[info] Life Simple, 0.03663177758433025
[info] Voting, 0.040667324754022724
[info] Mousetraps, 0.05816919018768013
[info] 3D Surface, 0.06135069069780657
[info] Urban Suite - Structure from Randomness 2, 0.06436046177967228
[info] GenDrift P global, 0.06930239379042409
@nicolaspayette
nicolaspayette / FizzBuzzUsingEither.scala
Last active August 29, 2015 14:06
Scala FizzBuzz using Either
object FizzBuzz extends App {
def check[A](properties: (A ⇒ Boolean, String)*)(x: A): String =
properties
.foldLeft(Left(x.toString): Either[String, String]) {
case (acc, (p, s)) ⇒
if (p(x)) Right(acc.fold(_ ⇒ s, _ + s))
else acc
}
.merge
@nicolaspayette
nicolaspayette / gist:1260949
Created October 4, 2011 05:11
Genetic Algorithm for choosing players in a hockey pool...
object Pool {
abstract sealed class Pos
case object Forwards extends Pos
case object Defensemen extends Pos
case object Goaltenders extends Pos
val posCounts: Map[Pos, Int] = Map(
Forwards -> 9,
Defensemen -> 6,
Goaltenders -> 2)