Coder Social home page Coder Social logo

adamnemecek / expressions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mpangburn/expressions

0.0 2.0 0.0 3.53 MB

Arithmetic and logical expressions elegantly modeled and visualized using protocol-oriented binary trees. ๐ŸŒณ

License: MIT License

Swift 99.46% Objective-C 0.54%

expressions's Introduction

Expressions

Twitter: @pangburnout

Arithmetic and logical expressions elegantly modeled and visualized using protocol-oriented binary trees with value semantics.

Features

  • Model arithmetic and logical expressions using binary trees.
  • Write expressions in code in the same manner as they would be written for evaluation, i.e. making full use of literals and both unary and binary operators.
  • Visualize expressions by rendering image representations, which can be easily seen using the Xcode Playground QuickLook feature.
  • Animate the evaluation of expressions using UIViews, and observe these animations using the Xcode Playground Live View feature.
  • Demonstrate the power of protocol-oriented programming by creating other simple tree structures, such as binary search trees, which gain QuickLook visualization for free.

Contents

The principal focus of this project is to demonstrate the structure and evaluation of arithmetic and logical expressions in an elegant, expressive way. Thanks to Swift's powerful ExpressibleBy*Literal protocols and operator overloading, we can write code like this:

let expression: ArithmeticExpression<Int> = 2*(1+3)-8/4

and in doing so create the full tree representing this expression, which in turn can be visualized in an Xcode Playground via QuickLook:

Furthermore, we can animate the evaluation of this expression by calling animateEvaluation(of:) in a Playground page. We can observe this animation in the Playground's Live View:

In addition to arithmetic expressions, logical expressions can be similarly created, viewed, and animated.

let expression: LogicalExpression = !(true || false) && false || !true

As a simple demonstration of the power of protocol-oriented programming, I've also implemented a couple of other tree structures, including a traditional binary search tree and a red-black tree, which can be visualized with QuickLook.

Getting Started

While a brief outline of the project's contents is provided in the section above, this is a Playground-based project and ultimately better demonstrated than explained:

  1. Clone the project.
  2. Open Expression.xcworkspace.
  3. Build the project.
  4. Within the Expression Playground, navigate to the ArithmeticExpression page to begin.
  5. See the magic through Xcode Playground's QuickLook and Live View features. Each Playground Page demonstrates a type of expression or other tree-based structure.

License

Expressions is released under the MIT license. See LICENSE for details.

If you find this project to be a useful tool in learning or teaching expressions or binary trees, please reach out to me on Twitter--I'd love to hear from you.

expressions's People

Contributors

mpangburn avatar adamnemecek avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.