Skip to content

Instantly share code, notes, and snippets.

@aadishv
Last active December 1, 2024 21:02
Show Gist options
  • Select an option

  • Save aadishv/086a318c4620261d105d93fcf665ca45 to your computer and use it in GitHub Desktop.

Select an option

Save aadishv/086a318c4620261d105d93fcf665ca45 to your computer and use it in GitHub Desktop.
made for a competition against my cousin where I proved swift's superiority over java :)
//
// ContentView.swift
// utilities
//
// Created by Aadish Verma on 11/7/24.
//
import SwiftUI
import Foundation
import Expression
typealias Expression = NumericExpression
class Point: Hashable {
let x: Int
let y: Int
init(x: Int, y: Int) {
self.x = x
self.y = y
}
static func == (lhs: Point, rhs: Point) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
// calculator app
func evaluateExpression(expression: String) -> Double? {
let exp = Expression(expression)
return try? exp.evaluate()
}
struct ContentView: View {
@State var currentState = ""
// grid:
// Clear * open paren * close paren * divide
// 7 * 8 * 9 * multiply
// 4 * 5 * 6 * subtract
// 1 * 2 * 3 * add
// 0 * . * delete * equal
static var actions: [Point: (String, (String) -> String, KeyboardShortcut)] = [
Point(x: 0,y: 0): ("C", { _ in "" }, KeyboardShortcut(.delete, modifiers: .command)),
Point(x: 1,y: 0): ("(", { $0 + "(" }, KeyboardShortcut("(", modifiers: [])),
Point(x: 2,y: 0): (")", { $0 + ")" }, KeyboardShortcut(")", modifiers: [])),
Point(x: 3,y: 0): ("÷", { $0 + "/" }, KeyboardShortcut("/", modifiers: [])),
Point(x: 0,y: 1): ("7", { $0 + "7" }, KeyboardShortcut("7", modifiers: [])),
Point(x: 1,y: 1): ("8", { $0 + "8" }, KeyboardShortcut("8", modifiers: [])),
Point(x: 2,y: 1): ("9", { $0 + "9" }, KeyboardShortcut("9", modifiers: [])),
Point(x: 3,y: 1): ("×", { $0 + "*" }, KeyboardShortcut("*", modifiers: [])),
Point(x: 0,y: 2): ("4", { $0 + "4" }, KeyboardShortcut("4", modifiers: [])),
Point(x: 1,y: 2): ("5", { $0 + "5" }, KeyboardShortcut("5", modifiers: [])),
Point(x: 2,y: 2): ("6", { $0 + "6" }, KeyboardShortcut("6", modifiers: [])),
Point(x: 3,y: 2): ("−", { $0 + "-" }, KeyboardShortcut("-", modifiers: [])),
Point(x: 0,y: 3): ("1", { $0 + "1" }, KeyboardShortcut("1", modifiers: [])),
Point(x: 1,y: 3): ("2", { $0 + "2" }, KeyboardShortcut("2", modifiers: [])),
Point(x: 2,y: 3): ("3", { $0 + "3" }, KeyboardShortcut("3", modifiers: [])),
Point(x: 3,y: 3): ("+", { $0 + "+" }, KeyboardShortcut("+", modifiers: [])),
Point(x: 0,y: 4): ("0", { $0 + "0" }, KeyboardShortcut("0", modifiers: [])),
Point(x: 1,y: 4): (".", { $0 + "." }, KeyboardShortcut(".", modifiers: [])),
Point(x: 2,y: 4): ("⌫", { String($0.dropLast()) }, KeyboardShortcut(.delete, modifiers: [])),
Point(x: 3,y: 4): (
"=",
{ do { return try evaluateExpression(expression: $0)?.description ?? $0} catch { return $0 } },
KeyboardShortcut(.return, modifiers: [])
)
]
var body: some View {
VStack {
TextField("Math the night away", text: $currentState)
.font(.title.monospacedDigit())
.multilineTextAlignment(.center)
.frame(width: 100*4)
.padding()
ForEach(0..<6) { ypos in
HStack {
ForEach(0..<4) {xpos in
ZStack {
if let (label, action, shortcut) = Self.actions[Point(x: xpos, y: ypos)] {
Button(action: {
currentState = action(currentState)
}) {
Text(label).padding().font(.title.monospacedDigit())
}.keyboardShortcut(shortcut)
.mask(RoundedRectangle(cornerRadius: 10))
}
else {
EmptyView()
}
}.frame(width: 100, height: 100)
}
}
}
}
//Periodic()
//MyExperimentalView()
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment