Skip to content

Instantly share code, notes, and snippets.

View erikerlandson's full-sized avatar

Erik Erlandson erikerlandson

View GitHub Profile
@erikerlandson
erikerlandson / testJO.scala
Last active February 26, 2024 18:42
An example quadratic programming (QP) optimization using JOptimizer in Scala
object testJO {
// libraryDependencies += "com.joptimizer" % "joptimizer" % "4.0.0"
import com.joptimizer.functions.PDQuadraticMultivariateRealFunction
import com.joptimizer.functions.PSDQuadraticMultivariateRealFunction
import com.joptimizer.functions.ConvexMultivariateRealFunction
import com.joptimizer.functions.LinearMultivariateRealFunction
import com.joptimizer.optimizers.OptimizationRequest
import com.joptimizer.optimizers.JOptimizer
// solution space is dimension n; in this example n = 2
@erikerlandson
erikerlandson / symbol.scala
Created January 31, 2023 23:36
get the value type of a scala 3 symbol as a TypeRepr
def symbolValueType(using Quotes)(
sym: quotes.reflect.Symbol
): quotes.reflect.TypeRepr =
import quotes.reflect.*
val TermRef(tr, _) = sym.termRef: @unchecked
// widen is important here otherwise you may get singleton or by-name types
tr.memberType(sym).widen
@erikerlandson
erikerlandson / podman-s2i-dir.sh
Created February 19, 2021 23:19
Example of chaining s2i podman image build using --as-dockerfile
#!/bin/bash
set -e
if [ $# -lt 2 ]; then
echo "usage: $0 <src-dir> <img-tag> [ <s2i-args> ... ]"
exit 1
fi
fail() {
echo $1
import java.lang.{ClassLoader, Package}
def allDefinedPackages(loader: ClassLoader): Vector[Package] =
if (loader == null)
Vector.empty[Package]
else
allDefinedPackages(loader.getParent()) ++ loader.getDefinedPackages()
def allClassPathURLs(loader: ClassLoader): Vector[java.net.URL] =
loader match
@erikerlandson
erikerlandson / audit2allow_example.txt
Created May 7, 2014 16:20
Example of using audit2allow to generate an selinux patch rule from failure messages in the audit log
# grep brprintconf_mfc /var/log/audit/audit.log | audit2allow -M mypol
# semodule -i mypol.pp
object code:
import scala.quoted.*
trait Context[T]:
val t: Int
given Context[Int] with
val t = 7
transparent inline def aMacro[T]: Int = ${ aMacroImpl[T] }
@erikerlandson
erikerlandson / ray_pi.py
Created April 22, 2021 14:21
SimpleRDD implemented in Ray
def ray_pi(n = 1000, k = 10):
c = SimpleRDD(range(n*k), k=k) \
.map(lambda _: (random.uniform(-1,1), random.uniform(-1,1))) \
.filter(lambda p: p[0]*p[0] + p[1]*p[1] <= 1) \
.count()
return 4 * c / (n*k)
package repro
trait ResZero[V]:
type Res
object ResZero:
transparent inline given [V]: ResZero[V] = ${ macros.resZeroImpl[V] }
end ResZero
trait SomeTrait[V]:
type Res
// This module isn't really a ScalaModule, but we use it to generate
// consolidated documentation using the Scaladoc tool.
object docs extends ScalaModule {
def scalaVersion = "3.0.0-M3"
def docSource = T.source(millSourcePath)
def moduleDeps = Seq(
...
)
package org.apache.spark.countSerDe
import org.apache.spark.sql.catalyst.util._
import org.apache.spark.sql.types._
import org.apache.spark.sql.Row
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.GenericInternalRow
import org.apache.spark.sql.expressions.MutableAggregationBuffer
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction