Scala Fun: Passing function signature as argument to another function

How to pass function signature as argument to another function in Scala? Well, I was looking for this answer myself recently and even though I found couple good examples in the wild I figured it won’t hurt putting together another one.

Let’s say we want to code simple calculator that only does operations on 2 numbers. We want to define rules we can easily plug in to our calculator and as long as the rules require only 2 numbers to perform the operation on, our calculator will happily use it. This may not be the best way of designing a calculator but for the sake of brevity I think it is good enough ;)

object main {
    object basicRules {
        def add(x: Double, y: Double) = x + y
        
        def multiply(x: Double, y: Double) = x * y
        
        def divide(x: Double, y: Double) = x / y
    }
    
    object advancedRules {
        /**
         * assumed x belongs to R
         * assumed y belongs to N
         * x,y must be > 0 otherwise result = -1
         */
        def power(x: Double, y: Double) = {
            var result = if (x > 0 && y > 0) x else -1
            for (i <- 2 to y.toInt) {
                result = result * x
            }
            result
        }
    }

    class Calculator {
        def calculate(x: Double, y: Double, f: (Double, Double) => Double) = f(x, y)
    }

    val calculator = new Calculator
    
    calculator.calculate(5, 5.1, basicRules.multiply)
    // > res0: Double = 25.5
    
    calculator.calculate(2, 3, advancedRules.power)
    // > res1: Double = 8.0
}

How to use IPython notebook with Dropbox

Recently I fell in love with IPython notebook. I installed it on my PC, laptop and my station at work. Very quickly I realized that sometimes I need to access notebook files from work pc at home and vice versa. Since all notebook files are saved locally you have to copy it over to another machine in order to access it.
One way to fix the problem is to setup IPython notebook on remote server and that way all your notebooks are in one place. Another way is to tell your local notebook to look into remote location for files.
Since Dropbox integrates well with Linux by mounting its resource into filesystem it is and ideal service for this purpose. It may be possible to use Google Drive as well but as far as I know it is a little bit more involved.
Continue reading

How to export MySQL database or single table into a file and load it back in

Recently I was working on optimising our internal application at work that is heavily MySQL driven. I was focusing on reworking SQL statements and making sure we are utilizing indexes properly. Modyfing and toying around with tables on live server is less than ideal so I needed a way to reliably and quickly move the data elsewhere so I can steadily experiment and fine tune sql statements and tables. Instead of using available phpmyadmin export options I decided to work directly with mysql which gives you more controll.
Continue reading

Editing your website files live in Linux/Ubuntu without FTP client

Whenever I work on small web projects I tend to edit files live to speed up the process. I used to use Filezilla FTP client for this purpose although every time you’re saving the file, Filezilla asks you if you want to overwrite the file. This becomes a little bit annoying over time.
On another note, If you’re developing on Windows I suggest you look at WinSCP free FTP client that will let you edit file live and won’t bug you every time you’re saving it.
Continue reading

Web Project: Zywiec USA

Zywiec USA

Zywiec USA

Client: Amtec Intl of NY
www.zywiecusa.com

Project description:
While working for Amtec in the advertising department, my manager asked me if I could create a website for them. The objective was to create a clean and slick website for Polish Beer fans. The website was to contain information about beers from Zywiec Brewery, user registration and a listing with upcoming events related to the product. The website also has a simple and custom-made Content Management System (CMS) to provide administration over registered users and gives the possibility to send HTML formatted e-mails.

Used Technologies:
XHTML, CSS, XML, Flash, jQuery, PHP, MySQL

Web Project: Orent Design

Orent Design

Orent Design

Client: Orent
www.orentdesign.com

Project description:
Orent is a design and renovation company located in NY. Initially they hired me to take photos and make panoramas of some of the apartments they worked on. Soon after, they asked me if I could work on the website too. Wojtek and Adam, the brains of the firm are both amazing guys. The first time we met, they already knew what they were looking for. The idea was to bring a very clean and bright web design to focus more on their accomplishments in the portfolio section of the site.

Used Technologies:
XHTML, CSS, jQuery, PHP

Web Project: Damis Restaurant

Damis Restaurant

Damis Restaurant

Client: Damis Restaurant
www.damisrestaurant.com

Project description:
My first project after moving to the US was creating a website for Damis Restaurant. The website you see here is actually a second reincarnation that was created 2 years after the first website. The design was completely changed to match the restaurant’s feel. It includes a simple, custom-made CMS that gives the ability to post News/Events and control entries in the Guestbook. Additionally, the 360 degree panorama was made to show the restaurant from the inside.

Used Technologies:
XHTML, CSS, PHP, MySQL

Bilingual websites made easy

Most of us would probably agree that if you wanted to create a single language website and wanted it to be accessible to the broadest audience, you should write the content in the English language. Although the English language has become international, there are still many countries where it is not spoken. What do you do if you want your website to be easily accessible for viewers from specific nationalities? You simply include multilingual content on your website.
Continue reading